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, ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Park City, UT/Albuquerque, NM/Flint, MI
    Posts
    3

    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);
    Thank you for your help!
    Matrixaffiliate

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Park City, UT/Albuquerque, NM/Flint, MI
    Posts
    3
    I had originally wrote the program with fread()/fwrite() but when I sent the finished program to my manager he sent it back with instructions to use something other than those function calls.

    I've heard of CopyFile() but I was under the impression that it was a C++ only command and didn't cross over into straight C. I take it that I'm mistaken on that?

    Thanks for the heads up on "wb+". I hadn't realized that it was superfluous when the original data is going to be scrapped.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Matrixaffiliate View Post
    I've heard of CopyFile() but I was under the impression that it was a C++ only command and didn't cross over into straight C. I take it that I'm mistaken on that?

    Thanks for the heads up on "wb+". I hadn't realized that it was superfluous when the original data is going to be scrapped.
    I don't think anything in the Windows API is C++.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    CopyFile Function (Windows)

    Why does your manager think that would be better?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2009
    Location
    Park City, UT/Albuquerque, NM/Flint, MI
    Posts
    3
    Apparently the program will be moving files that are several GB in size, and he doesn't want the program to read through each file individually per say.

    Thanks a lot Mats and tabstop. I really appreciate your help. I'll start running with CopyFile() and see if that's more along the lines of what my manager is looking for.

    Thanks again for your help and advice. I really appreciate it.

    Matrixaffiliate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  3. Program speed and function calls
    By thetinman in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 01:29 AM
  4. C++ and Java System Calls
    By Korn1699 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2004, 03:31 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21