C++ file stream open modes ambiguity

前端 未结 2 1907
我寻月下人不归
我寻月下人不归 2021-02-07 09:36

For performing file IO in C++ we use the ofstream, ifstream and fstream classes.

  • ofstream: Stream class to write on files
  • ifstream: Stream class to r
2条回答
  •  伪装坚强ぢ
    2021-02-07 09:42

    Because the mode is not limited to input/output. The constructor of ifstream, for example, looks like:

    explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );
    

    Note the default value is ios_base::in, so you don't have to specify it yourself. However, the mode sets the streams flags which are not limited to in/out, but include:

    • app (append) Set the stream's position indicator to the end of the stream before each output operation.
    • ate (at end) Set the stream's position indicator to the end of the stream on opening.
    • binary (binary) Consider stream as binary rather than text.
    • in (input) Allow input operations on the stream.
    • out (output) Allow output operations on the stream.
    • trunc (truncate) Any current content is discarded, assuming a length of zero on opening.

提交回复
热议问题