Thread: Why is there one extra byte when copying file?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    Why is there one extra byte when copying file?

    Hi guys, am new in C programming. I don't understand the following:

    In the below code snippet, input.txt is 30 bytes. When I run the program, output.txt is 31 bytes.

    Code:
    FILE *fp, *new_fp;
    int ch;
    
    if((fp = fopen("input.txt", "rb")) == NULL){
    	fprintf(stderr, "Error opening file for reading...");
    	exit(1);
    }
    
    if((new_fp = fopen("output.txt", "wb")) == NULL){
    	fprintf(stderr, "Error opening a new file for writing!");
    	exit(1);
    }
    
    /* I use a while loop to check for FEOF */
    while(!feof(fp)){
    	ch = fgetc(fp);
    	putc(ch, new_fp);
    }
    
    fcloseall();


    In another code snippet as shown below, input.txt and output.txt is both 30 bytes.

    Code:
    FILE *fp, *new_fp;
    int ch;
    
    if((fp = fopen("input.txt", "rb")) == NULL){
    	fprintf(stderr, "Error opening file for reading...");
    	exit(1);
    }
    
    if((new_fp = fopen("output.txt", "wb")) == NULL){
    	fprintf(stderr, "Error opening a new file for writing!");
    	exit(1);
    }
    
    while(1){
    	ch = fgetc(fp);
    	if(!feof(fp)){
    		fputc(ch, new_fp);
    	}else{
    		break;
    	}
    }
    As you can see above, the only different is the while looping to copy data from one file to another.

    Can someone enlighten me why there is a extra one byte in the first code snippet?

    Thanks in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because the loop in your first program is broken. See the FAQ on "why feof shouldn't be used to control a loop".

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Because the loop in your first program is broken. See the FAQ on "why feof shouldn't be used to control a loop".
    Thanks for your fast reply!

    The link @ Cprogramming.com FAQ > Why it's bad to use feof() to control a loop was very helpful indeed.

    Thanks! Now i know why. LOL!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xbfish View Post
    Thanks! Now i know why. LOL!
    You missed a great "...and knowing is half the battle!" opportunity there. But I guess you probably weren't born in time to know. :oldguy:


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why sizeof a macro has always an extra byte?
    By limp in forum C Programming
    Replies: 3
    Last Post: 07-07-2011, 03:03 PM
  2. File Comparision byte by byte
    By anusha2489 in forum C Programming
    Replies: 12
    Last Post: 05-16-2011, 06:58 AM
  3. Output file has extra characters.
    By fadlan12 in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2010, 08:33 PM
  4. appending extra byte to a char *
    By apus29 in forum C Programming
    Replies: 5
    Last Post: 03-23-2010, 08:59 AM
  5. Extra line break added during copying
    By Tigers! in forum C Programming
    Replies: 14
    Last Post: 10-21-2009, 04:49 AM