Thread: HELP: feof

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    HELP: feof

    heylo guys! im new here, just registered today!! yay!!!

    ok im trying to write a program that takes an input file, reads its character by character and writes to a output file character by character.

    now the problem is for example: input = 1 2 3 4 5, output = 1 2 3 4 5 4

    and i really can't figure out why except maybe i dont know how to use feof...
    here is the code, please help!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
    	char* infile = argv[1];
    	char* outfile = argv[2];
    
    	FILE* inptr = fopen(infile, "r");
    	FILE* outptr = fopen(outfile, "w");
    
    	
    
    	int data;
    	while(!feof(inptr))
    	{
    		
    		fread(&data, sizeof(int), 1, inptr);
    		fwrite(&data, sizeof(int), 1, outptr);
    	}
    	
    	fclose(inptr);
    	fclose(outptr);
    	return 0;
    }

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Do you zero your buffer in between write and read?

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    wow that fixed it! but really dont know why...??
    but now the output file has a bunch of empty spaces at the end. u know why??

    thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't be controlling your loop with feof. Instead you should be doing:
    Code:
    while( fread ... )  == numberofobjectsyoumeantoread )
    {
        fwrite( ... );
    }

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

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by dlf723 View Post
    wow that fixed it! but really dont know why...??
    but now the output file has a bunch of empty spaces at the end. u know why??

    thanks
    Run through it in your head.
    Read 1: Overwrite whole buffer with "1 2 "
    R2: "3 4 "
    R3: Overwrite first 2 bytes with "5 " leaving "5 4 "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. feof issues
    By Rare177 in forum C Programming
    Replies: 13
    Last Post: 11-23-2004, 10:11 AM
  2. feof stuff...
    By cnewbee in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 06:26 PM
  3. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 12:38 PM
  4. advice on feof()
    By Hobo in forum C Programming
    Replies: 2
    Last Post: 09-07-2002, 08:15 AM
  5. Is !feof supposed to work?
    By SMurf in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 12:44 PM