We recommend using Visual Studio 2017

Constructing Input Stream Objects

 

Visual C++ in Visual Studio 2015

The latest version of this topic can be found at 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:

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 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  

Input Streams

Show: