Loop question

This is a discussion on Loop question within the C++ Programming forums, part of the General Programming Boards category; I have a program that reads in different text files, with random numbers of lines of text. How do i ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Loop question

    I have a program that reads in different text files, with random numbers of lines of text.

    How do i setup a loop to stop the program reading in lines of text when it gets to the end of the text file ?

    Currently my program gets to the last line, and then continues to read that line in over and over again.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Like this
    Code:
    while(file>>input)
    {
        //stuff
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This is what I use:
    Code:
    ifstream myInputFile("C:\\TestData\\input.txt");
    string input;
    
    while(!myInputFile.eof())
    {
    	getline(myInputFile, input);
    	cout<<input<<endl;
    }
    Last edited by 7stud; 04-26-2005 at 08:40 AM.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    prog-bman's code will read in the contents of a file until an error occurs or EOF is reached.
    7stud's code, however, reads in 10 lines. If you're lucky, the file has 10 or more lines. If not, you've got yourself a headache with that code.

    If you're looking to get input a line at a time instead of a word at a time, you can put getline() into the condition of a while loop. Like the stream extraction operator, getline() will return false if an error occurs or EOF is reached.

    In conclusion, use prog-bman's method.

    edit: 7stud changed his code, but it's still wrong. For the reasons why, check the FAQ. You should still use prog-bman's method instead.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    We can then follow the problem to that function, and we find that most read functions will set EOF once they've read all the data, and then performed a final read resulting in no data, only EOF.
    Is getline() one of those functions?
    Last edited by 7stud; 04-26-2005 at 09:04 AM.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    getline() returns a reference to an istream just like the extraction operator does.
    An istream returns false when an error condition or EOF is reached.

    A quick Google search confirms it:
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
    http://www.tek-tips.com/faqs.cfm?fid=2516
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    An istream returns false when an error condition or EOF is reached.
    ...and?

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    And that means
    Code:
    vector<string> vec1;
    string line;
    vec1.clear();
    ifstream infile ("stl2in.txt");
    while (getline(infile,line,'\n'))
    {
      vec1.push_back (line);
    }
    works exactly the way you want it to!

    FAQ > Explanations of... > Definition of EOF and how to use it effectively
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...hmmm. I fail to see your point since the while loop I posted does what I want it to.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    FAQ > Explanations of... > Definition of EOF and how to use it effectively
    http://faq.cprogramming.com/cgi-bin...0&id=1043284351
    The eof() method returns true or false, so that faq is not on point.

  11. #11
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    eof() only returns true when it thinks the end of the file has been reached. It doesn't return true if an error occured reading from the file. There's more than one way that a call to the extraction operator or getline() could fail, and eof() only knows of one of them. That's why it's unwise to use it. It's even worse to misinform someone else.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    eof() only returns true when it thinks the end of the file has been reached. It doesn't return true if an error occurred reading from the file. There's more than one way that a call to the extraction operator or getline() could fail, and eof() only knows of one of them.
    Well now you're changing your story. The while loop I posted checks for the end of file flag, and no claims were made otherwise. The question was:
    How do i setup a loop to stop the program reading in lines of text when it gets to the end of the text file ?
    There's more than one way that a call to the extraction operator or getline() could fail, and eof() only knows of one of them. That's why it's unwise to use it.
    Some would argue it's bad programming practice to lump the end of file test in with a test for other input stream errors.

    It's even worse to misinform someone else
    You mean like claiming getline() won't hit the end of file properly? Or, claiming eof() won't work properly based on a faq that doesn't even mention that method?

    In any case, I don't think misinforming others is the high crime and misdemeanor you do, if you are trying to provide them with information in good faith and you end up being wrong.

  13. #13
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    http://www.horstmann.com/cpp/pitfalls.html

    You're method is listed on a page entitled C++ Pitfalls. Have you also noticed you're the only one arguing with me? I'm not generally one to argue from authority, but you won't listen to reason.

    Some might argue that the tests should be performed seperately, but I don't know any good programmer who thinks testing for end-of-file is sufficient. Yet you show only that method to someone who is learning to program. That simply isn't right.

    I wish I could still dole out some negative rep ...
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  14. #14
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I wish I could still dole out some negative rep ...

    [sarc] Stud has a sweet tooth, and I think he would like the red candy he gets [sarc/]


  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    but you won't listen to reason.
    Well, I don't think that is true. I'm always trying to learn, but just because someone doesn't accept your argument doesn't mean they aren't listening and trying to critically determine whether it is a valid argument or not. In this case, I haven't been able to discern any valid reasons in your post. Your latest argument is a case in point:

    http://www.horstmann.com/cpp/pitfalls.html

    You're method is listed on a page entitled C++ Pitfalls.
    The code sample at that link uses the >>operator. The code I posted uses getline(). The >>operator works differently then getline().

    Have you also noticed you're the only one arguing with me?
    I'm discussing a C++ issue on a C++ forum, and if numbers are the indicator of who is right, I would have to observe that you are also a team of 1.

    I wish I could still dole out some negative rep ...
    Why? You posted some things, I posted the reasons why I felt they weren't correct, and you started insulting me, yet I haven't responded in kind. I have a feeling they suspended the reputation system to prevent just that kind of abuse.
    Last edited by 7stud; 04-26-2005 at 12:04 PM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 09:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-30-2008, 12:12 AM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 12:43 AM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 06:13 PM

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