Constructing Input Stream Objects
Visual Studio 2015
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
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
voidargument constructor, then call theopenmember 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
Show: