ios_base::ate and tellp()

A

Alex Vinokur

#include <fstream>
#include <iostream>

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

std::eek:fstream 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

12
0


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


Alex
 
V

Victor Bazarov

#include <fstream>
#include <iostream>

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

std::eek:fstream 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)



12
0


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

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
 
A

Alex Vinokur

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

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

Melzzzzz

Replacing std::eek:fstream fs2(fileName, std::ios_base::ate) with
std::eek:fstream 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.
 
A

Alex Vinokur

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


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

Melzzzzz

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.

I think that app, tells stream not to truncate file.
You can try to reposition write pointer on app.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top