Muokkaa

Jaa


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

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:

string s("123.45");

double amt;
istringstream myString(s);

//istringstream myString("123.45") also works
myString>> amt; // amt contains 123.45

See also

Input Streams