Codingfile copy in C

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  ALL
33919.1 
I need to copy a file in C.

Everything on Google is explaining about reading file contents into a buffer and stuff.

I don't want to fuck about re-inventing shit, I just want to copy.

Any idea how I run cp or whatever I need to just specify source and destination and let something else do it?
0/0
 Reply   Quote More 

 From:  Mal (BAD)  
 To:  Peter (BOUGHTONP)     
33919.2 In reply to 33919.1 
If you're running on Windows then you need the CopyFile function.

If not then I have no idea.

EDIT: CopyFile not FileCopy

back in a flash! and as dangerous as ever...
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Mal (BAD)     
33919.3 In reply to 33919.2 
Nope, this is for linux.

I've found the system() function which allows system calls, and I've got this at the moment:

C code:
void file_copy(char source_file[] , char dest_file[])
{
	char cmd[1000];
	sprintf(cmd , "cp -p %s %s" , source_file , dest_file);
	system(cmd)
}



Now I just need to figure out why my source argument appears to be coming in as blank. :?
0/0
 Reply   Quote More 

 From:  Mal (BAD)  
 To:  Peter (BOUGHTONP)     
33919.4 In reply to 33919.3 
This why Windows is such a success - developer references are freely available and second to none. I expect you're supposed to go and trawl through someone else's source code in the hope of finding what you need...

back in a flash! and as dangerous as ever...
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Mal (BAD)     
33919.5 In reply to 33919.4 
Um, Windows is such a success because of Microsoft's aggressive marketing.

The problem here is nothing to do with the OS, it's entirely the fault of C being a really shit language.
0/0
 Reply   Quote More 

 From:  Dave!!  
 To:  Peter (BOUGHTONP)     
33919.6 In reply to 33919.5 
Well, use ColdFusion instead then :P
---

The intelligence of American Politics

"We won't know where Osama is hiding until we find him." - Donald Rumsfeld
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Dave!!     
33919.7 In reply to 33919.6 
I'd use Java if I had enough space to put it on there... very tempting to get a bigger card so that I can.


Although there isn't a multi-threaded example for Java, so I'd need to figure that out also.
(Assuming that multi-threading will allow me to actually read/copy the file which I'm locking... don't know if it actually will.)
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Peter (BOUGHTONP)     
33919.8 In reply to 33919.3 
It's really ever so much easier to just read from one stream and write to another. But you were mean about Windows and C, so I'm not going to give you the couple of lines of code which will do it.

Dance like it hurts; Love like you need the money; Work when people are watching.
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Peter (BOUGHTONP)     
33919.9 In reply to 33919.3 
Oh, and char[1000]? Amateur.

Dance like it hurts; Love like you need the money; Work when people are watching.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  THERE IS NO GOD BUT (RENDLE)     
33919.10 In reply to 33919.8 
I was not mean about Windows, and C deserves it.
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  THERE IS NO GOD BUT (RENDLE)     
33919.11 In reply to 33919.9 
No, there was a 'cmd' in there too!

----
some things never change

0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Peter (BOUGHTONP)     
33919.12 In reply to 33919.10 
You said Windows was successful because of marketing, which is plainly untrue. Windows is successful because it was better than OS/2.

And C is the greatest language ever, unless you're trying to use it as a scripting language, of course. If all you're going to do is system() stuff, you might as well use Python.

Anyway,

C code:
#include <stdio.h>
 
void filecopy(char *src, char *dest)
{
  FILE *fsrc = fopen(src, "r");
  FILE *fdest = fopen(dest, "w");
 
  int c;
  while ((c = getc(fsrc)) != EOF)
  {
    putc(c, fdest);
  }
  
  fclose(fdest);
  fclose(fsrc);
}

You really will find life in C much easier if you use pointers instead of arrays.

Dance like it hurts; Love like you need the money; Work when people are watching.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  THERE IS NO GOD BUT (RENDLE)     
33919.13 In reply to 33919.12 
(hug)

That's much better than all the stuff Google was giving me.


Since I couldn't find any real software that does CDP properly, I'm trying to write my own with Dazuko.
What I want to do initially is simple (onWrite, copy file), but everything comes in as an 'OPEN' event, so I've got to figure out which flags/modes indicate opening for writing, and then work out if I can actually allow a read request to come in whilst write requests are locked, which is going to throw me into looking at threading and stuff. :&
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  THERE IS NO GOD BUT (RENDLE)     
33919.14 In reply to 33919.12 
YOUR FUNCTION IS FAULTING MY SEGMENTS! :@


Get a "Segmentation fault" unless I comment out everything except the first three lines - even just attempting to open and close the files fails. :(



Heh, seperated function into its own app, and it appears to be related to doing filecopy("~/myfile","~/mycopy") - if I change those to argv[1] and argv[2] and pass the exact same text on the command line, it works.
Am I missing something really basic/obvious? :S
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Peter (BOUGHTONP)     
33919.15 In reply to 33919.14 
To be honest, it's been so long since I've used fopen in a *nix environment that I can't remember if the ~ notation will work. I do know that it gets expanded to the home folder by the shell before it gets processed as argv. What happens if you hardcode the folder in the C? (Obviously it's not a solution, but it would determine whether it was worth trying to remember how to read environment variables and construct the full path...)

Dance like it hurts; Love like you need the money; Work when people are watching.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  THERE IS NO GOD BUT (RENDLE)     
33919.16 In reply to 33919.15 
Ah, that's it! Thankyou!

In this case it is a solution - just getting things working first, and then I'll be converting it all to variables and ...whatever the equivalent of .ini/.cfg is, so it'll be a once only operation to set directory paths.
0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Peter (BOUGHTONP)     
33919.17 In reply to 33919.16 
I love the way a simple "Invalid filename" gets conflated to a "Segmentation fault". It's one of my favourite things about Linux C.

quote: Segmentation Violation: Core Dumped Blues
Well, my terminal's locked up,
and I ain't got any Mail,
And I can't recall the last time
that my program didn't fail.
I've stacks in my structs;
I've got arrays in my queues;
I've got the:
Segmentation violation -- Core dumped... blues.

Now if you think that it's nice
that you get what you C,
Then go: illogical statement...
with your whole family!
Cause the Supreme Court ain't the only place
with: Bus error... views;
I've got the:
Segmentation violation -- Core dumped... blues.

Well on a 2 Gig Core 2 Duo,
life should be a breeze,
But with Fedora in the house,
even magnetic tapes would freeze!
Now you might think that unlike Red Hat,
I'd know who I abuse;
But I've got the:
Segmentation violation -- Core dumped... blues.

Dance like it hurts; Love like you need the money; Work when people are watching.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  THERE IS NO GOD BUT (RENDLE)     
33919.18 In reply to 33919.17 
I can't get the rhythm of that, can you sing it for me? :)
0/0
 Reply   Quote More 

 From:  Matty (EVSTAR)  
 To:  Peter (BOUGHTONP)     
33919.19 In reply to 33919.18 
I think its rap
0/0
 Reply   Quote More 

 From:  Serg (NUKKLEAR)  
 To:  Matty (EVSTAR)     
33919.20 In reply to 33919.19 
I imagined Elvis singing it :(
[...Insert Brain Here...]
0/0
 Reply   Quote More 

Reply to All  
 

1–20  21

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats