Thread: Yet Another Getline Question

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Yet Another Getline Question

    Sorry to rehash old topics, but I couldn't find a relevant post to my question. I have smashed together some code that does what I want, but I'm not quite sure why. The problem I'm trying to solve is printing out text from an input file, redirected to the program using this syntax:

    ./a.out < input.txt

    I understand how to solve this using C, by reading the characters until an EOF value is read in. But this whole stream thing is confusing to me. Here is my current code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main(){
    
    
       string line;
    
    
       while( getline( cin , line ) )
       {
          cout << line << endl;
       }
    
    
       return 0;
    }
    My main questions:

    1. What is the return value of the getline function?

    2. Is this return value from getline causing the while loop to terminate? Or is there some eof flag magic happening somewhere that I don't know about?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What is the return value of the getline function?
    O_o

    You should have already learned how to search the web.

    Open your browser to your favorite search engine and search for "C++ getline string".

    Is this return value from getline causing the while loop to terminate? Or is there some eof flag magic happening somewhere that I don't know about?
    That is not an "either or" question. The `getline' is "causing the while loop to terminate" because "there [is] some eof flag magic happening somewhere".

    basic_ios::operator bool - C++ Reference

    If you click on the various contextual links, you'll find how using a "stream" in a Boolean context coerces the result in the form of the opposite of `std::basic_ios::fail()', which is a member of all "streams", allowing you to check for "stream" validity as part of read/write operations.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    So if I'm understanding correctly, the return value of getline is a boolean value, which is the opposite value of std::basic_ios::fail. And std::basic_ios::fail returns true if eofbit is set, and because the return value of getline is the opposite of the return value of std::basic_ios::fail, it returns false. And thus this ends the while loop.

    Does that sound correct?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jwroblewski44
    the return value of getline is a boolean value
    Not quite: the return value of getline can be converted to a boolean value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jwroblewski44 View Post
    So if I'm understanding correctly, the return value of getline is a boolean value, which is the opposite value of std::basic_ios::fail. And std::basic_ios::fail returns true if eofbit is set, and because the return value of getline is the opposite of the return value of std::basic_ios::fail, it returns false. And thus this ends the while loop.

    Does that sound correct?
    std::getline - cppreference.com
    The documentation says specifically that getline return an istream and that it returns the input (i.e., the stream you told it to read from).
    Taking a look at istream (std::basic_istream - cppreference.com), we see that it overloads an operator bool (std::basic_ios:perator bool - cppreference.com), which says that the return value of this operator is "true if the stream has no errors, false otherwise."
    You will also notice that it says "2) Returns true if the stream has no errors and is ready for I/O operations. Specifically, returns !fail()."
    You will also see at the bottom what the different functions, fail(), etc, returns depending on the state of the stream.
    Further looking at the getline documentation says that it sets eof or fail bits under various conditions.

    So there's your answer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Thanks. More reading on streams is definitely required.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about getline and cin
    By jimgeor in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2012, 07:53 AM
  2. Question About Getline
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2009, 05:13 PM
  3. question abt getline
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 07:50 PM
  4. getline question
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2003, 06:17 AM
  5. STL question: getline
    By *ClownPimp* in forum C++ Programming
    Replies: 5
    Last Post: 09-29-2002, 12:20 PM