C++ Filehandling: Difference Between iOS::App and iOS::Ate

C++ Filehandling: Difference between ios::app and ios::ate?

It’s the other way around. When ios::ate is set, the initial position will be the end of the file, but you are free to seek thereafter. When ios::app is set, all output operations are performed at the end of the file. Since all writes are implicitly preceded by seeks, there is no way to write elsewhere.

What is the difference between ios::app and ios::ate?

app comes from 'append' - all output will be added (appended) to the end of the file. In other words you cannot write anywhere else in the file but at the end.

ate comes from 'at end' - it sets the stream position at the end of the file when you open it, but you are free to move it around (seek) and write wherever it pleases you.

ofstream open modes: ate vs app

You need to combine std::ios::in with std::ios::ate then seek the end of the file and append the text:

Consider I have a file "data.txt" which contains this line:

"Hello there how are you today? Ok fine thanx. you?"

Now I open it:

1: std::ios::app:

std::ofstream out("data.txt", std::ios::app);

out.seekp(10); // I want to move the write pointer to position 10

out << "This line will be appended to the end of the file";

out.close();

The result is not what I wanted: No moving the write pointer but only the text is always appended to the end.

2: std::ios::ate:

std::ofstream out2("data.txt", std::ios::ate);

out2 << "This line will be ate to the end of the file";

out2.close();

The result above is not what I wanted no text appended but the content is truncated!

To solve it combine ate with in:

std::ofstream out2("data.txt", std::ios::ate | std::ios::in);

out2 << "This line will be ate to the end of the file";

out2.close();

Now the text is appended to the end but what is the difference:

As I said app doesn't allow moving the write pointer but ate does.

std::ofstream out2("data.txt", std::ios::ate | std::ios::in);

out2.seekp(5, std::ios::end); // add the content after the end with 5 positions.

out2 << "This line will be ate to the end of the file";

out2.close();

Above we can move the write pointer to the position we want while with app we cannot.

  • Look at this example: https://www.youtube.com/watch?v=6cDcTp1gn4Q

What's the difference between opening a file with ios::binary or ios::out or both?

ios::out opens the file for writing.

ios::binary makes sure the data is read or written without translating new line characters to and from \r\n on the fly. In other words, exactly what you give the stream is exactly what's written.

Why does tellp() return 0 for ios::app but not for ios::ate?

From std::ios::openmode (Section 27.5.3.1.4 of C++11)

std::ios::app means to seek to the end of stream before each write. So stream may be not be at end before any write operation. No matter where the pointer is (0 or somewhere else) writing is always done at end. (Implicit seek to end before each write operation)

on the other hand std::ios::ate means to seek to the end of stream immediately after open and thus is guaranteed to return the size of file.

Further reading: C++ Filehandling: Difference between ios:app and ios:ate?

What is ios::in|ios::out?

  • ios::in allows input (read operations) from a stream.
  • ios::out allows output (write operations) to a stream.
  • | (bitwise OR operator) is used to combine the two ios flags,

    meaning that passing ios::in | ios::out to the constructor

    of std::fstream enables both input and output for the stream.

Important things to note:

  • std::ifstream automatically has the ios::in flag set.
  • std::ofstream automatically has the ios::out flag set.
  • std::fstream has neither ios::in or ios::out automatically

    set. That's why they're explicitly set in your example code.


Related Topics



Leave a reply



Submit