Ignoring line in C++ when reading from file

This is a discussion on Ignoring line in C++ when reading from file within the C++ Programming forums, part of the General Programming Boards category; Hi all! I have the following codes i use to read from a textfile. Code: while(!textfile.eof()) { //textfile is a ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    Ignoring line in C++ when reading from file

    Hi all!

    I have the following codes i use to read from a textfile.

    Code:
    while(!textfile.eof())
      { //textfile is a ifstream                      
         textfile >> string1 >> string2;
         //use strings for processing
       }
    however my above program hits an error when there is a blank line in the text file. i would only want to process the information if only the link is not blank. i have tried a couple of ways but they couldnt work.. is there anyway for me to check if the line is blank using the ifstream textfile that i got? and only if the line is not blank can i go process the data.

    Thanks in advance

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Read the FAQ.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    That code should skip blank lines correctly. Can you post a more complete example that demonstrates the problem?

    BTW, using eof() to control the loop that way is a bad idea. It will often lead to processing of the last line twice. NOrmally you would use this:
    Code:
    while(textfile >> string1 >> string2)
      { //textfile is a ifstream                      
         //use strings for processing
       }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Use the string version of getline, if the line was empty, then the resulting string will have a length of 0. If not, then you can try to extract the string1/string2 values from the line you've just read. To do that, you could then initialize a stringstream object with the line you've already read and then >> the data from the stringstream just like you currently do with the fstream(?) object.

    Code:
    while(!textfile.eof())
    You should avoid the testing of eof in the while loops condition. There are only a few instances where such a thing works. The extraction itself (in whatever form it takes) should instead be put in its place, i.e.:
    Code:
    while( textfile >> string1 >> string2 )
    Or...:
    Code:
    while( getline(textfile,line) )
    The reason for this is that eof is not set to true until after an unsuccessful read attempt has been made. If you've just read the last line of data from the file (a successful read), then eof is still false even though there really is no more data. You haven't read past that point to trigger eof to be true yet. Then end result of this is that you go through the loop one more time than you should be doing. This subsequent read inside the loop does fail and eof gets set to false but you are already inside the loop processing data you think is valid (the string1 and string2 variables that you think are going to be holding valid data). The variables are likely to have the same values in them as they did in the last successful read and your output/result (if any) are not going to be accurate.
    Last edited by hk_mp5kpdw; 11-15-2007 at 06:31 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    operator >> will skip over all whitespace, including multiple newlines, so you really shouldn't have to switch to getline. Of course, if you want to read in strings with embedded spaces, then you do want to switch.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Hi this is my code that got error when encounter blank lines.

    Code:
    while(!textFile.eof())
      {                                                           
            if(line == 0)
             {                  
               textFile >> string1 >> string2;
             }
            else if(line == 1)
             {
                textFile >> string1 >> string2 >> string3;
              }   
            else
            {
                //no action   
             }     
            line++;
    }
    Anything wrong with above? why did i get error when the datafile got a blank line?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Code:
    while(!textFile.eof())
    Is usually a bad thing, but in this case, it may succeed. Suggest you read the FAQ about that and beware using it.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    What does your input file look like?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM

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