Eof??

This is a discussion on Eof?? within the C Programming forums, part of the General Programming Boards category; Hi its me the student again. This time i read a file but the machine cannot tell if the end ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    Eof??

    Hi its me the student again.

    This time i read a file but the machine cannot tell if the end of the file has been reached.

    i am using


    FILE *fp /* pointing to a stream */


    while(fp!= EOF)
    {
    do so and so
    }

    When i open the stream which is a text file, the last line read from another file is repeated forever, if i dont user break the program.

    Please help, thank you Ganonshin. Bye.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To test a file to see if it's already read to or past EOF, you can use 'feof', which is not the preferred method.

    A better method is to test the return value of your read function. Check the return value of fgetc, fread, fscanf, or whatever it is you're using to read.

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

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Hi Quzah.

    I am using fscanf to read the file - how do i check the return value and when i have the value what do i do with it??

    Hope you respond.

    Ganonshin, bye for know.

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    From Salem's post in the C++ Forum

    Code:
    void good ( char *in, char *out ) {
        FILE *fin = fopen( in, "r" );
        FILE *fout= fopen( out,"w" );
        int ch;
        while ( (ch=fgetc(fin)) != EOF ) {
            fputc(ch,fout);
        }
        fclose(fin);
        fclose(fout);
    }
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Thanks Azuth, b ut i dont really understand it.

    Thanks anyway Ganonshin.

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The key to testing for eof is in this loop

    Code:
    while ( (ch=fgetc(fin)) != EOF ) {
            fputc(ch,fout);
    }
    I haven't used fscanf much, but it appears to have a number of limitations that fgetc does not. If you switched to fgetc it then becomes a case of how you deal with the data once read from the file.

    What is in the text file? Is it delimted? What do you do with the text when you read it in?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    The basic format is this

    Code:
    while ( fscanf( ... ) != EOF) {
      /* Do something with it..or not */
    }
    Any input function can replace the scanf() functions, such as fgetc(), fgets(), etc.

    But as Salem often says do not do this
    Code:
    while (!feof(fileptr)){
      /*blah blah*/
    }
    Because it reads the last line twice.

    kwigibo

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >But as Salem often says do not do this...
    and quite rightly too. Here's an example of bad coding.
    Code:
    #include <stdio.h>
    #define FILENAME "text.txt"
    int main(void)
    {
    	char buffer[1024];
    	FILE *fileptr;
    	int lines = 0;
    	
    	if ((fileptr = fopen(FILENAME, "r")) == NULL)
    	{
    		perror(FILENAME);
    		return (1);
    	}
    	
    	while (!feof(fileptr))
    	{
    		fgets(buffer, 1024, fileptr);
    		lines++;
    		printf ("Line %d of the file is %s", lines, buffer);
    	}
    	
    	fclose(fileptr);
    	
    	return (0);
    }
    Create a text file called text.txt with say 5 lines of data in it. You'll find that you actually get 6 lines of output. Well, at least that's what happens under some compilers. I have found that some do handle it differently. Therefore it is best not to use code like this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    What I found that worked (someone can tell me if this sounds incorrect), I get a char from the input file. Then I test for EOF using feof. Then if the file is not at the end I put the char back to the input stream.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Hi everyone, I cant remeber who sugested to get the return value but it worked, a big cheers if you know who you are.

    Thanks from Ganonshin.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 03:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 04:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 06:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21