The "ate" mode!

May 24, 2013 at 3:54pm
Hey guys! I am confused about that guy called the "ate mode" in file handling. How do I use it? I mean, it didn't work the way I read it should.

Also I want to know how I can delete or edit a file in C++?

Thanks in advance!
Last edited on May 24, 2013 at 3:55pm
May 24, 2013 at 4:04pm
He probably meant ios::ate mode. It will place cursor position at end file after opening, so following:
std::ostream x("file.txt", ios::out|ios::ate)
is equals to
1
2
std::ostream x("file.txt", ios::out)
x.seekp(0, ios::end);


It main difference from ios::app, that it doesn't perform seeking before each output operation, so you can still write in the middle of file.

http://en.cppreference.com/w/cpp/io/ios_base/openmode
Last edited on May 24, 2013 at 4:07pm
May 25, 2013 at 2:12pm
Errr... I don't get your point! Can you explain it in full detail? Thanks!
The link you referred to wasn't much help either! What use is it actually?
Last edited on May 25, 2013 at 2:19pm
May 25, 2013 at 2:37pm
When you open a file it is ready to be written at its beginning. If you open it with ios::ate you will be ready to write at its end, but can move around it and write to other parts of the file. If you open it with ios::app you will be able to write ONLY at its end, because each operation will place you back at its end before being executed.

To delete a file you can use http://www.cplusplus.com/reference/cstdio/remove/
To edit a file there is no immediate way. Any write operation performed in the middle of a stream will overwrite as much old data as what you're trying to write.
You have to open a new empty file, copy all the data until you get to the part that you want to edit, write the changes, and then copy the rest of the old file. Then remove the old file and rename the new.
May 25, 2013 at 2:44pm
ios::ate seeks to the eof(end of file) upon opening the file. the input/output operations can still occur anywhere within the file......i.e. u can write anywhere in the file,even over the old data.
The format of using is:
objectname.open("Nameofthefile",ios::ate)

this is how the "ate mode" works. !!
May 26, 2013 at 5:50am
Thanks! That pretty much explains everything! :)
Topic archived. No new replies allowed.