getline() help

This is a discussion on getline() help within the C++ Programming forums, part of the General Programming Boards category; I am having problems with a program I am trying to write. I am using the getline() method to read ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    getline() help

    I am having problems with a program I am trying to write. I am using the getline() method to read lines of a file. It functions correctly until a blank line is encountered. Then it stops executing. Is there a way I can catch an exception or something to skip the blank line and continue on the other side?
    Here is the section of my code causing the problems. I'm positive it's the getline() statement that's causing the problem. It can't get an empty line.
    Code:
    while(end != true)
         {
    
           getline(sceneFile, fileBuffer);
           tokens = tokenizer(fileBuffer, tokens);
    I'd appreciate some help. It's been a long time since I've used c++. Been more of a java junkie for some time.


    Thanks,
    Stephen

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    This is what I generally do -

    Code:
    while ( getline( MyFileStream, MyStringVariable ) )
    {
      // do stuff
    }
    That will keep on reading till it gets to the end of the file. If it encounters a blank line, just check -

    if ( MyStringVariable == "" ) // do something about it
    Last edited by twomers; 09-23-2006 at 01:57 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Thanks for the quick reply, twomers.

    I'm not sure that will fix my problem though.
    The difficulty I'm having is that getline(fs, sv) is throwing some sort of exception that is causing execution to end immediatly.

    I'll try that though and see what happens.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    cout<< sv;

    and see what happens, in the while loop.

    Check to see if SV=="" as well


    EDIT - [COLOR="#f1f1f1"]Ol&#233;, 1000 posts!![/COLOR]
    Last edited by twomers; 09-23-2006 at 02:08 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Thanks man,
    I actually just got this to work by putting an
    Code:
    if(fileBuffer.empty()) //Checks for empty line
             continue;

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> while ( getline( MyFileStream, MyStringVariable ) )
    This checks for errors, but getline succeeds even if the line is empty. It is still a good idea to do it this way anyway, and then add the check for the empty string.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> This checks for errors, but getline succeeds even if the line is empty. It is still a good idea to do it this way anyway, and then add the check for the empty string.

    Or put into code:

    Code:
    while ( getline( F_S, S_V ) )
    {
        if ( S_V == "" ) // or the empty() thing. EDIT - Whoops, I had a == 0 first 
        {
            // Do stuff
        }
    }
    should do it
    Last edited by twomers; 09-23-2006 at 02:23 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    10
    Thanks guys.
    My code is working well now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

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