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