Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 953
Adam_G Avatar asked Apr 28 '12 01:04

Adam_G


People also ask

What is the meaning of file mode parameter ios :: ate?

ios::ate. Opens an existing file (either input or output) and seeks to the end.

Why do we use ios app in C++?

Apple provides Objective-C++ as a convenient mechanism for mixing Objective-C code with C++ code. Objective-C is close to C but with object-oriented features implemented as a thin layer on top of C. It's a strict superset of C which makes any C code a valid Objective-C program.

What happens when we open a file in ios :: trunc mode and ios :: app mode?

ios::app (short for append) means that instead of overwriting the file from the beginning, all output operations are done at the end of the file. This is only meaningful if the file is also open for output. ios::trunc (short for truncate) means that when the file is opened, the old contents are immediately removed.

What is ios :: binary in C++?

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.


1 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.

like image 88
Jon Purdy Avatar answered Sep 22 '22 08:09

Jon Purdy