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

102,721

Solution 1

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.

Solution 2

They are specified as follows (in 27.5.3.1.4 of C++11):

app seek to end before each write

ate open and seek to end immediately after opening

With ios::app the write position in the file is "sticky" -- all writes are at the end, no matter where you seek.

Solution 3

It is pretty good documented here.

ios::ate "sets the stream's position indicator to the end of the stream on opening."

ios::app "set the stream's position indicator to the end of the stream before each output operation."

This means the difference is that ios::ate puts your position to the end of the file when you open it. ios::app instead puts it at the end of the file every time you flush your stream. If, for example, you have two programs that write to the same log file, ios::ate will override anything that was added to the file by the other program since your program opened it. ios:app will instead jump to the end of file each time your program adds a log entry.

Solution 4

ios::app--> "We cannot move the pointer. It will be only at end."

ios::ate--> "We can move the record pointer to any other place."

Solution 5

The ios::ate option is for input and output operations and ios::app allows us to add data to the end of file.

Share:
102,721

Related videos on Youtube

Adam_G
Author by

Adam_G

Updated on November 05, 2021

Comments

  • Adam_G
    Adam_G over 2 years

    What's the difference between ios::ate and ios:app when writing to a file.
    In my view, ios::app gives you the ability to move around in the file, whereas with ios::ate it can only read/write at the end of the file. Is this correct?

    • L. F.
      L. F. almost 5 years
      By the way, it is really ios_base::ate and ios_base::app.
  • harvpan
    harvpan almost 6 years
    Can you provide a link to documentation?
  • 23r0c001
    23r0c001 over 2 years
    I should have mentioned that a shortcoming of opening file with ios::in | ios::ate won't work if the file doesn't already exist.