Why the eof returns false when it should return true?

This is a discussion on Why the eof returns false when it should return true? within the C++ Programming forums, part of the General Programming Boards category; I have written this piece of code: Code: if(command.is_open() == true) { while(command.eof() == false) { command >> instr; switch(instr) ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Why the eof returns false when it should return true?

    I have written this piece of code:
    Code:
    	if(command.is_open() == true)
    	{
    		while(command.eof() == false)
    		{
    			command >> instr;
    			switch(instr)
    			{
    			case 'r':
    				command >> x;
    				//readDataFile(x, hdList);
    				break;
    			}
    		}
    	}
    where command is my file stream from text file, which looks exactly like this one:
    Code:
    r
    1
    The program is supposed to read the first line, and if it is 'r' then read also the second one which is the parameter for the 'r' instruction. At first, the eof returns false which is correct, however, after getting '1' from the file, and breaking from the switch instruction it goes back to the while statement where the eof evaluates false again, while after grabbing the last 1 it should evaluate for true. The result I am getting is that the program iterates through the 'r' switch two times, where it should be just one. How to make the eof evaluate true after getting the '1' from the file?
    Last edited by kulfon; 02-06-2011 at 02:35 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,694
    The file doesn't matter so much, eof() is only true after EOF is reached.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    So how do I make it work? I do not want it to iterate through one of the switch statements twice after grabbing the last instruction from the file. Should I add another while(command.eof() == false) after the command >> instr;?

    edit: naaah, does not work
    Last edited by kulfon; 02-06-2011 at 02:50 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,694
    Well you have to try reading in order to enter EOF state anyway, so try

    while (command >> instr)

    If the read fails for any reason, the loop will exit. Then you may call eof() to determine if the file ended or if it was just a bad read.

  5. #5
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by whiteflags View Post
    Well you have to try reading in order to enter EOF state anyway, so try

    while (command >> instr)

    If the read fails for any reason, the loop will exit. Then you may call eof() to determine if the file ended or if it was just a bad read.
    ^this......the next problem that would arise is if the instruction read from the file doesn't match any of the cases the ifstream won't read twice in one iteration so in the next iteration command >> instr would read what's suppose to go into the x variable. you should remove the command >> x from the switch statement unless you have code handling that sort of exception in the default case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. BIG problem. How do I find a bug in so much code?
    By Yarin in forum C++ Programming
    Replies: 44
    Last Post: 01-31-2008, 11:39 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

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