Testing a stream object in a while statement

This is a discussion on Testing a stream object in a while statement within the C++ Programming forums, part of the General Programming Boards category; Hi, I am puzzled by this fact that how a stream object be tested in a while statement. A stream ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Unhappy Testing a stream object in a while statement

    Hi,
    I am puzzled by this fact that how a stream object be tested in a while statement. A stream object by itself does not return anything. If it does carry a "value" where is that value stored?...Could someone please explain
    For example
    Code:
    ifstream infile("TEST.TXT");
    while(infile)
    {
    //do something
    }
    How does a stream object take "values" and how does it get changed when certain operations are done on this object.
    Also, do all objects carry a "value"?

    Thanks
    sudar

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    A stream object by itself does "return" something. In a boolean context, it evaluates to true if all is fine, or false if an error occurred or it's at the end of the stream.
    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
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sudar View Post
    Also, do all objects carry a "value"?
    In the case of streams that works via a conversion operator.
    something like this
    Code:
    #include <iostream>
    using namespace std;
    
    class xstream {
    public:
        xstream() : flag(0) { }
        void set_error() { flag = 1; }
        operator const void *() { return flag ? 0 : this; } 
    private:
        int flag;
    };
    
    int main() {
       xstream m;
       if ( m )
            cout << "ok." << endl;
       else
            cout << "error." << endl;
    
       m.set_error();  // simulate error
       
       if ( m )
            cout << "ok." << endl;
       else
            cout << "error." << endl;
    }
    Kurt

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Technically the stream operator will return !fail(). This means that if the stream is at the end of the file, even if it sets the eofbit, it will still return true unless the failbit is also set.

    It just so happens that most often when a read causes the eofbit to be set it does so during failed read which also sets failbit.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    True. My fault.
    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

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Hi,
    Thanks for the replies...so...is this true for non-stream objects too?...In other words, any object of any class will return a value when tested?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    No, this is only true of classes that implement this technique. The stream classes are just one group of classes that do. Most classes do not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 02:08 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 08:49 AM
  3. Finding object code
    By kidburla in forum C Programming
    Replies: 3
    Last Post: 11-29-2005, 01:09 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  5. Testing object pointer for null
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2002, 11:51 AM

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