Using system calls with C program

This is a discussion on Using system calls with C program within the C Programming forums, part of the General Programming Boards category; I'm writing a program for work that needs to copy a file from a directory on one drive to another, ...

Using system calls with C program

I'm writing a program for work that needs to copy a file from a directory on one drive to another, e.g. from a flash drive to the hard drive. Originally I had planned on simply opening the file and using fread to read through the file and then using fwrite to "recreate" the file on the new drive. That code worked fine, but it doesn't do well for large files. A friend suggested using a system call to copy and paste the file from one drive to the other, but everything I'm finding on it refers to UNIX environments and there are several different examples. Since this program will only ever be used on Windows machines can I still use this system call idea and if I can, would you mind giving me some example code of where to start. From the White Bible to Google I've seen enough code that I've managed to confuse myself royally.

This is the code I have right now.

Code:

FILE *in;
FILE *out
fopen=in("E:\\folderA\\%s"fileA, "rb") //fileA is a *char[]
fopen=out("C:\\folderB\\fileB", "wb+")
/*System Call to copy paste
This is where I originally had my fread command
and my fwrite command. */
fclose(in);
fclose(out);

a loop doing fread()/fwrite() of a chunk of data, would be the right way to solve this - at least if you want to be reasonably portable. Bear in mind that if your chunk is not 1 byte [and that would be terribly inefficient], the last packet may not be complete.

If you make your chunk large enough (say somewhere between 8 and 64KB - multiples of 4KB will help efficiency, preferably aligned to 4KB as well) it should be fairly close to "as efficient you can get".

Note that reading from a flash-device is SLOW in general, so that's going to be the big bottleneck.

Using system calls will very marginally improve that. Windows has a CopyFile() function that may be useful for what you want to do.

Using "wb+" seems a bit unnecessary if you have no intention of keeping the original data, by the way.