• Popular Now

• 12
• 11
• 9
• 10
• 13

Archived

This topic is now archived and is closed to further replies.

Copying a file

This topic is 5451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am trying to write a program that will simply copy one file. I have the following simple C program, but the output file always contains one extra byte than the input file. If I look at an output file in an editor, it appears to be an extra newline character. Can anyone spot the problem?

#include <stdio.h>

int main (int argc, char * argv[]) {
FILE	* infile, * outfile;
char	c;

if (argc != 3) {
printf("You forgot to enter the filenames\n");
return 1;
}

infile = fopen(argv[1], "rb");

if (!infile) {
printf("Cannot open input file\n");
return 1;
}

outfile = fopen(argv[2], "wb");

if (!outfile) {
printf("Cannot open output file\n");
return 1;
}

while (!feof(infile)) {
c = getc(infile);
putc(c, outfile);
}

fclose(infile);
fclose(outfile);

return 0;
}


Share on other sites

  while (!feof(infile)){		c = getc(infile);		putc(c, outfile);	}

While not the end of file, do the following:

Get C, and place C in a file.

What if The file ends at getc? You''re placing an invalid character in the file. when you get C, make sure thats not the end of file.