Why does "ifstream" open for both reading and writing?

Today I saw an interesting piece of code:

ifstream fil;

fil.open( "ini.txt", std::ios::in | std::ios::out );

I was just about to talk about my weakness, but, to my surprise, I saw that cppreference.com seems to think that this is correct:

http://en.cppreference.com/w/cpp/io/basic_ifstream/open

mode - indicates the open stream mode. This is a bitmask type, the following constants are defined:

  • in: open to reading
  • out: open for recording

How can it ifstream, which, as I understand it, is an INPUT file stream, is opened both for reading and writing?

Is this not necessary fstreaminstead ifstream?

+4
source share
2 answers

std::ifstream - std::basic_filebuf. std::basic_ifstream::rdbuf.

( , ). . ifstream ofstream. . , , , :

rdbuf()->open(filename, mode | ios_base::in)

, .

+1

fstream:

fstream file("input.txt", ios::in | ios::out | ios::app);
int data;

file >> data;
file << data +1;

file.close();
0

Source: https://habr.com/ru/post/1688133/


All Articles