Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ios_base::ate and tellp()

39 views
Skip to first unread message

Alex Vinokur

unread,
Jan 23, 2013, 2:26:11 AM1/23/13
to
#include <fstream>
#include <iostream>

int main()
{
const char* fileName = "out1";
std::ofstream fs1(fileName);
fs1 << "AAAAAAAAAAA\n";
std::cout << fs1.tellp() << std::endl;
fs1.close();

std::ofstream fs2(fileName, std::ios_base::ate);
std::cout << fs2.tellp() << std::endl;
fs2.close();

return 0;
}

gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)

> g++ file02.cpp

> ./a.out

12
0


Why does fs2.tellp() print 0, but not 12?


Alex

Victor Bazarov

unread,
Jan 23, 2013, 8:50:33 AM1/23/13
to
I think the 'ate' might actually count not from the start of the file
but from the initial position, one beyond the end. Since you're going
to be writing after the end, the contents of the file prior to your
writing are considered irrelevant. Maybe. I am not sure, this is just
my speculation.

V
--
I do not respond to top-posted replies, please don't ask

Alex Vinokur

unread,
Jan 23, 2013, 9:40:30 AM1/23/13
to
Replacing std::ofstream fs2(fileName, std::ios_base::ate) with std::ofstream fs2(fileName, std::ios_base::in | std::ios_base::ate) produces expected output.
But why do we need std::ios_base::in here ?

Melzzzzz

unread,
Jan 23, 2013, 9:44:12 AM1/23/13
to
On Wed, 23 Jan 2013 06:40:30 -0800, Alex Vinokur wrote:

>
> Replacing std::ofstream fs2(fileName, std::ios_base::ate) with
> std::ofstream fs2(fileName, std::ios_base::in | std::ios_base::ate)
> produces expected output.
> But why do we need std::ios_base::in here ?

Because file gets truncated if not. You can also try with std::ios::app.

Alex Vinokur

unread,
Jan 23, 2013, 9:51:15 AM1/23/13
to
app -> (append) Set the stream's position indicator to the end of the stream before each output operation.


ate -> (at end) Set the stream's position indicator to the end of the stream on opening.




Melzzzzz

unread,
Jan 23, 2013, 10:21:30 AM1/23/13
to
I think that app, tells stream not to truncate file.
You can try to reposition write pointer on app.
0 new messages