Parsing/EOF() problem

This is a discussion on Parsing/EOF() problem within the C++ Programming forums, part of the General Programming Boards category; Hi. The problem i have is with a program that is using while(!eof()) to read a file, and do actions ...

  1. #1
    IonBlade
    Guest

    Parsing/EOF() problem

    Hi.
    The problem i have is with a program that is using while(!eof()) to read a file, and do actions based on the first character of a line.

    Here is a very simple version of my code:

    Code:
    while(!DFile.eof())
    {
       DFile.getline(templine,1);
       cout<<templine;
    }
    system("PAUSE");
    the problem is, the program runs up to this point, then when it gets to the while loop the cursor just sits there blinking, and the program does not continue no matter how long i wait or what buttons i push. all i can do is shut down the program. Any help as to why this is happening?

  2. #2
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    woops, didn't log in correctly
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try putting a cout<<endl; between the end of the loop and the pause statement.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    Doesn't work.

    here's what i did:
    Code:
    while(!DFile.eof())
    {
       DFile.getline(templine,1);
       cout<<templine;
       cout<<endl;
    }
    system("PAUSE");
    the program now prints blank lines forever, never reaching the pause statement as if it's not reaching the end of the file. The file isn't large at all, so i don't know why it isnt getting to the end of it.

    I also tried putting the cout<<endl outside of the bracket right before the pause statement, but that didn't do anything at all
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What is templine declared as? And what compiler/OS are you using?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    Code:
    char templine[72];

    using win98, and dev-c++ v4.9.7.5 compiling with mingw32
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use this:
    Code:
    	while( !DFile.eof( ) ) {
    		
    		DFile.getline( templine, 72 ); // Changed 1 to 72
    		cout<<templine<<endl;
    	
    	}
    The second parameter for getline is the size of the buffer, so you should set it to 72. If the line is longer than buffersize - 1 characters, then it seems to crash.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while( !DFile.eof( ) ) {
    This is not the way to control the loop. Use the return code from the read function to determine when you've hit EOF.

    >>DFile.getline(templine,1);
    Read the manual about the getline() function, as XSquared said, using 1 as the second parameter is incorrect.
    http://www.cppreference.com/cppio_details.html#getline
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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