Taitai
Taitai

Reputation: 616

what is the difference between ofstream and ifstream?

In ofstream you can set the mode to be read files, e.g.

 std::ofstream of(filename.c_str(), std::ofstream::in);

Then what is the difference between it and ifstream?

Upvotes: 0

Views: 1502

Answers (2)

xskxzr
xskxzr

Reputation: 13040

ofstream and ifstream are totally different classes. Although you can open the underlying file of ofstream under input mode, it does not support input methods such as operator>>, get and so on.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190897

ofstream is for output or writing files. ifstream is for input or reading files.

According to http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

  • out is always set for ofstream objects (even if explicitly not set in argument mode). Note that even though ofstream is an output stream, its internal filebuf object may be set to also support input operations

Upvotes: 2

Related Questions