Console C++ - fstream Template/Namespace Problems

fstream provides an operating system independent way to read and write files.


Visual C++

inFile.fail() equals 1 if the file does not exist.
inFile.eof() becomes non-zero at the end of file.

I am showing several examples here. I have personally tested these with Microsoft Visual C++ 6.0. Note: The problems here are due to mixing templates with regular header files.

The obvious solution is to use either all templates or all header files.

This example may seem a little strange, but apstring (used in high school classes) uses iostream.h. As a result, there can be problems. This shows one way to deal with them. It is also possible to use fstream.h is you omit std::.

This example uses a "string" variable for the filename, and a try/catch structure to see if the file exists. It also reads the text file one line at a time. This example uses a do..while loop and an alternate form of the getline function.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Console_C++ / fstream.html