Share via


Constructing Input Stream Objects

If you use only the cin object, you do not need to construct an input stream. You must construct an input stream if you use:

  • Input File Stream Constructors

  • Input String Stream Constructors

Input File Stream Constructors

There are two ways to create an input file stream:

  • Use the void argument constructor, then call the open member function:

    ifstream myFile; // On the stack
    myFile.open( "filename" );
    
    ifstream* pmyFile = new ifstream; // On the heap
    pmyFile->open( "filename" );
    
  • Specify a filename and mode flags in the constructor invocation, thereby opening the file during the construction process:

    ifstream myFile( "filename" );
    

Input String Stream Constructors

Input string stream constructors require the address of preallocated, preinitialized storage:

char s[] = "123.45";
double amt;
istrstream myString( s );
myString >> amt; // Amt should contain 123.45

See Also

Reference

Input Streams