ostream is a general purpose output stream. cout and cerr are both examples of ostreams. ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file. ofstream is an output file stream.
The class ofstream is used for output to a file. Both of these classes are defined in the standard C++ library header fstream . Here are the steps required for handling a file for either input or output: Create an instance of ifstream or ofstream .
The close() function is used to close the file currently associated with the object. The close() uses ofstream library to close the file.
You can try this:
#include <fstream>
int main () {
std::ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
The file streams are actually defined in <fstream>
.
You may not be including the appropiate header file.
adding #include <fstream>
at the beggining of your source file should fix the error.
I think it is a simple spelling mistake offstream instead of ofstream.
Probably, you are including the wrong header file. There is a header <iosfwd> that is used for header files that need to reference types from the STL without needing a full declaration of the type. You still are required to include the proper header <iostream> in order to use the types in question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With