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
    Fatinez
    Guest

    Cusom file copy prog

    Hi,

    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

    Code:
    FILE *src, *dst;
    char data[BUFFSIZE];
    
    src = fopen(argv[1],"rb");
    dst = fopen(argv[2], "wb");
    
    while(fgets(data,sizeof(data),src) != NULL)
        	fputs(data,dst);
    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
    Posts
    76
    Here's a helpful link:
    isDirectory() in C/C++
    Last edited by poccil; 04-07-2003 at 03:51 PM.

  3. #3
    Fatinez
    Guest
    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
    Last Post: 05-02-2008, 03:53 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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