By Robin

2014-04-24 02:39:14 8 Comments

What is the difference between:

fstream texfile;"Test.txt");


ofstream textfile;"Test.txt");

Are their function the same?


@Danqi Wang 2014-04-24 02:52:08

Take a look at their pages on here and here.

ofstream inherits from ostream. fstream inherits from iostream, which inherits from both istream and stream. Generally ofstream only supports output operations (i.e. textfile << "hello"), while fstream supports both output and input operations but depending on the flags given when opening the file. In your example, the open mode is ios_base::in | ios_base::out by default. The default open mode of ofstream is ios_base::out. Moreover, ios_base::out is always set for ofstream objects (even if explicitly not set in argument mode).

Use ofstream when textfile is for output only, ifstream for input only, fstream for both input and output. This makes your intention more obvious.

@Cheers and hth. - Alf 2014-04-24 02:54:24

y ! cite instead

@Danqi Wang 2014-04-24 02:58:58

@Cheersandhth.-Alf is prettier, but google returns me for c++ fstream. What are other advantages of over

@Cheers and hth. - Alf 2014-04-24 03:23:53

@DanqiWang: generally more correct due to better peer-review

@user657267 2014-04-24 02:41:44

ofstream only has methods for outputting, so for instance if you tried textfile >> whatever it would not compile. fstream can be used for input and output, although what will work depends on the flags you pass to the constructor / open.

std::string s;
std::ofstream ostream("file");
std::fstream stream("file", stream.out);

ostream >> s; // compiler error
stream >> s; // no compiler error, but operation will fail.

The comments have some more great points.

@M.M 2014-04-24 02:45:32

Also, ofstream::open defaults to openmode ios_base::out, and fstream::open defaults to ios_base::in | ios_base::out

@0x499602D2 2014-04-24 02:45:47

Moreover, in the case of std::fstream, attempting to open the file will fail if the file does not exist. This is in contrast to std::ofstream which creates a file if one cannot be found. One would have to add the std::ios_base::trunc flag to the mask when calling the constructor or open() on an std::fstream.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] What is the "-->" operator in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2386890 View
  • 4243 Score
  • 1 Answer
  • Tags:   c++ c++-faq

28 Answered Questions

40 Answered Questions

10 Answered Questions

[SOLVED] What is the difference between g++ and gcc?

  • 2008-10-05 20:25:13
  • Brian R. Bondy
  • 459434 View
  • 881 Score
  • 10 Answer
  • Tags:   c++ gcc g++

19 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 528949 View
  • 1833 Score
  • 19 Answer
  • Tags:   c++ linux profiling

8 Answered Questions

[SOLVED] Difference between &#39;struct&#39; and &#39;typedef struct&#39; in C++?

  • 2009-03-04 20:41:12
  • criddell
  • 524312 View
  • 839 Score
  • 8 Answer
  • Tags:   c++ struct typedef

15 Answered Questions

5 Answered Questions

[SOLVED] What is the copy-and-swap idiom?

17 Answered Questions

[SOLVED] What should main() return in C and C++?

Sponsored Content