Sunday, December 05, 2004

C++

http://www.stanford.edu/class/cs106b/winterhandouts/H06%20C++%20streams.pdf

fs.open(filename.c_str());

The above line attempts to open the file with the given filename (searching in the current directory). Note that open member function takes in a char* argument and not a C++-style string, so you must use c_str to convert it. (Sigh, this is just a weird artifact of the stream libraries being completed before the string library). Opening the file may or may not succeed (depending on whether the named file exists, what permissions it has, etc.), so before you proceed, you should check the state of the stream.

if (fs.fail()) return;

The call to fail checks if the state of the stream is unhappy, in this case, it will determine if the file was properly opened. This is essential before we start trying to output to the stream (in the same way that we check if a FILE* is NULL in C).

1 comment:

Naradmuni said...

>>error: ISO C++ forbids defining types within return type

Your class in the header needs to be
terminated with a ';'. Otherwise you are still in the definition of the
class at...