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?
-
By the way, it is really `ios_base::ate` and `ios_base::app`. – L. F. Jul 17 '19 at 07:02
7 Answers
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.

- 53,300
- 8
- 96
- 166
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.

- 1
- 1

- 273,490
- 39
- 460
- 699
It is very well 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.
-
The link doesn't work. The documentation is available here: https://cplusplus.com/reference/fstream/ofstream/open/ – Hari Feb 27 '23 at 13:54
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."

- 54
- 2
- 6

- 137
- 1
- 2
I'm adding an answer here because I recently ran into a situation where ios::ate should have worked, but most online documentation on this topic, like this page on cplusplus.com, and similar answers on this post turned out to be only partially correct or at least misleading.
What everyone is correct about is that ios::app will open a file without deleting its contents, and move the cursor to the end of the file before EVERY write to the file. So using a function like seekp is more or less useless when a file has been opened with ios::app, because the cursor will move automatically to the end of the file with every write, before the writing occurs.
What else everyone is correct about is that ios::ate will not move the cursor to the end of the file before every write. So functions like seekp will work with ios::ate. Also technically correct is that ios::ate will move the cursor to the end of the file when you open it, but that is a technicality -- see next paragraph.
What does NOT line up with most documentation and forum answers is that in reality (please someone comment if they have a reasonable explanation for my experience here) opening a file with ios::ate will delete its contents, much like ios::trunc. Which leaves you with not a lot of great options if you want to open a non-empty file for writing, not delete its contents, and retain the ability to effectively move your stream position, as with seekp.
The only way I found to solve my problem was to use a solution I found in this Stack Overflow post, namely to open the file with flags (ios::in | ios::ate), even though I'm opening an ofstream for writing. This makes me somewhat uncomfortable to do but it's what I'm doing for now.

- 151
- 1
- 6
-
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. – 23r0c001 Nov 04 '21 at 17:55
The ios::ate
option is for input and output operations and
ios::app
allows us to add data to the end of file.

- 1,782
- 1
- 13
- 25

- 11
- 1
Try this code, and it'll help you understand (clear the old data first - I personally combine ios::in
and ios::ate
):
ofstream myFile("test.txt", ios::ate|ios::in);
myFile << "Hello World";
myFile.seekp(6, ios::beg);
myFile << "Hello World";
myFile.close();
Test with ios::app
, it has some differences.