#include%26lt;stdlib.h%26gt;
#include%26lt;stdio.h%26gt;
int copy_file(char *Old, char *New);
int main (void)
{
char source[1024], destination[1024];
/*get source file name*/
printf("\nplease enter the source file name: ");
gets(source);
/*get destination file name*/
printf("\nplease enter the destination file name: ");
gets(destination);
/*check value returned by copy function, if any other*/
/*vlaue other than 0 is returned error message is displayed*/
if (copy_file(source, destination) ==0)
puts("File copy was successful");
else
fprintf(stderr, "Error trying to copy file!");
return 0;
}
int copy_file(char *Old, char *New)
{
FILE *fo, *fn;
int read, written;
/*open source file for reading in binary mode*/
/*if fopen gives a NULL character -1 is returned*/
/*main and error message displayed*/
if((fo=fopen(Old, "rb"))==NULL)
return -1;
/*open the destination file in binary writting mode*/
/*if fopen returns NULL source file is closed, the value*/
/*-1 is returned to main and error message is displayed*/
if((fn = fopen(New, "wb"))==NULL)
{
fclose(fo);
return -1;
}
/*read from source file in blocks of 1024, if the end of*/
/* the source file has not been reached, write the block to*/
/*the destination file.*/
while (1)
{
read = fread(Old, 1, 1024, fo);
getchar();
written = fwrite(Old, 1, read, fn);
getchar();
if(!feof(fo))
fputc(written, fn);
else
break;
}
fclose(fo);
fclose(fn);
return 0;
}
C program ( It has some error) Pl correct them?
It would help if you told what the problem is - whether it doesn't compile or doesn't work, error messages, etc.
A quick look over doesn't show me anything seriously wrong, but I would say don't use gets, it is notoriously insecure (buffer overruns). There are other library functions that take a length argument, which closes that hole.
The fputc() is wrong, it takes the ascii for a character as the first arg, you are passing it (I believe) the number of bytes written, this will be garbage.
And I assume those 2 "getchar()"s are for debugging purposes?
easter cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment