Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ofstream error in c++

Tags:

c++

People also ask

What does ofstream stand for?

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.

What library is ofstream?

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 .

How do I turn off 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.