Thread: end of file

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    end of file

    I try reading a text file written in this format:

    p1 111 1111 pD1

    p2 222 2222 pD2

    p3 333 3333 pD3

    but the output just keep on looping
    the last line in the file after outputing 1st 2 line in the text file.

    my code
    is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip>
    
    
    main()
    {
    
    	
    
    	char pk[BUFSIZ];
    	char pD[BUFSIZ];
    
    	int e;
    	int n;
             
    
    
    
    	ifstream b_file("c:/check.txt");
    	
    
    	while ( b_file.eof != 0 )
    	{
    	
    
    			cout << "\n";
    
    			//flushall();
    	
    			b_file >> pk;
               
    			cout << pk;
    
              
    			cout << "\n";
    
    
    			b_file >> e;   
    	          
    			cout << e;
               
    	
    			cout << "\n";
    
    
    			b_file >> n;
                 
    			cout << n;
    
    
    			cout << "\n";
    
    
    			b_file >> pD;
                 
    			cout << pD;
    
    
    			cout << "\n";
    					
    	}
    
    
    	b_file.close();    
    
               
    	return(0);
    
    }

    Can anyone help me on this problem.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while ( b_file.eof != 0 )
    Try
    while ( ! b_file.eof() )

    eof() is a function, which means you need the () to actually call the function
    Simply writing eof means you get a pointer to the function. Function pointers are always non-NULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Thanks salem, but the output is:

    p1
    111
    1111
    pD1

    p2
    222
    2222
    pD2

    p3
    333
    3333
    pD3

    p3
    333
    3333
    pD3

    There is 1 extra output for the 3rd line, is there any way to solve it?

    i tried :
    > while ( EOF != 0 )

    but it also keep looping.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >There is 1 extra output for the 3rd line
    That's because b_file.eof() was not designed to be used as a loop condition and you loop one too many times. Because the input fails, you process whatever was in memory at the time, which is usually the last record read. The simplest solution is to assume that if the first element of a record is present, the entire record is:
    Code:
    while ( b_file >> pk ) {
      cout << "\n" << pk << "\n";
      ...
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Thanks the help Prelude!

    So > while ( b_file >> pk )
    means put all the info in the file into the variable pk until
    there is no more info to put into pk right?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Correct. But a better way of thinking about it would be to read the first item in a record. As long as that item is valid, the rest of the record must be there so you read it as well. Lather, rinse, repeat.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    Smile

    Thank for the info Prelude, your help is deeply appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 01:41 AM