Thread: return value for global getline method

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    return value for global getline method

    I am doing line-oriented file input in C++ using the global getline function defined in the <string> header. Various books demonstrate getline's idiomatic use:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
          .
          .
       std::ifstream infile("foo.txt");
          .
          .
       std::string aline;
       while (std::getline(infile, aline)) {
          // process line
          .
          .
       }
    The function prototype for getline is:
    istream& getline(istream& is, string& s, char delimiter = '\n');

    So getline returns a reference to istream. My question is, how can it be used in a while expression, which expects a boolean? I'm guessing some implicit conversion takes place, but it's not apparent to me from the standard library API docs.

    Thanks for your help.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's a conversion operator defined that converts an istream to a void*, which is further converted to a bool by using a simple != NULL rule. The void* converter will be a non-NULL value if the stream is ready and NULL if an error occurs.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM