Jump to content
  • Advertisement

Archived

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

Russell

Copying a file

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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

}


Think about this for a moment.

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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!