difference between ios::ate and ios::trunc

Mar 18, 2010 at 5:27am
Can anyone explain what the difference between using flag trunc and ate is?
It'd be nice if you could give some examples to show the difference.

By the way, what does "ate" stand for?

EDIT: I have replaced app with trunc, as what I really meant was what is the difference between trunc and ate?
Last edited on Mar 18, 2010 at 7:41pm
Mar 18, 2010 at 6:54am
std::ios::app doesn't truncate the file when it is opened, and std::ios::ate puts the file pointer at the end of the file. This is useful to get the file size:
1
2
std::ifstream file("file.txt",std::ios::binary|std::ios::ate);
unsigned long size=file.tellg();

Without std::ios::ate, you'd have to call file.seekg(0,std::ios::end) before line 2.
Last edited on Mar 18, 2010 at 6:56am
Mar 18, 2010 at 7:40pm
Well, I think I made a mistake. What I meant was
What is the difference between trunc and ate


But thanks anyways for the explanation!
Mar 18, 2010 at 9:44pm
Oops. I completely misread that.

std::ios::trunc ensures that the file will be truncated.
Topic archived. No new replies allowed.