Cusom file copy prog

This is a discussion on Cusom file copy prog within the C Programming forums, part of the General Programming Boards category; Hi, I have an assignment where i have to write a custom file copy program for UNIX. We are not ...

  1. #1

    Cusom file copy prog


    I have an assignment where i have to write a custom file copy program for UNIX. We are not allowed to use system() and cannon use the UNIX cp command in any way.

    command line looks like this:
    mycp orig dest

    orig is copied to dest.
    If dest does not exist, it is created. If it does exist, it is overwritten. If dest is a directory, orig is copied into that directory with the same file name, ie. c:\orig -> c:\dest\orig

    Now the problem:
    this code is simplified a lot

    FILE *src, *dst;
    char data[BUFFSIZE];
    src = fopen(argv[1],"rb");
    dst = fopen(argv[2], "wb");
    while(fgets(data,sizeof(data),src) != NULL)
    If dest exists, the file copies fine. But if dest doesn't exist, the file pointer is 0 and I get a segmentation fault when it tries to write to it.

    Also, how do I check if dest is a directory?

    Any help would be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Feb 2003
    Here's a helpful link:
    isDirectory() in C/C++
    Last edited by poccil; 04-07-2003 at 03:51 PM.

  3. #3
    Thanks a lot, poccil. I figured out the other problem and got the directory thing working. It now works to perfection.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a copy of binary file
    By aarti_gehani in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 03:49 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
  5. archive format
    By Nor in forum A Brief History of
    Replies: 0
