Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   ios_base::ate and tellp() (http://www.velocityreviews.com/forums/t956822-ios_base-ate-and-tellp.html)

Alex Vinokur 01-23-2013 07:26 AM

ios_base::ate and tellp()
 
#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 01-23-2013 01:50 PM

Re: ios_base::ate and tellp()
 
On 1/23/2013 2:26 AM, Alex Vinokur wrote:
> #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?


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 01-23-2013 02:40 PM

Re: ios_base::ate and tellp()
 
On Wednesday, January 23, 2013 3:50:33 PM UTC+2, Victor Bazarov wrote:
> On 1/23/2013 2:26 AM, Alex Vinokur wrote:
>
> > #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?

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


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 01-23-2013 02:44 PM

Re: ios_base::ate and tellp()
 
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 01-23-2013 02:51 PM

Re: ios_base::ate and tellp()
 
On Wednesday, January 23, 2013 4:44:12 PM UTC+2, Melzzzzz wrote:
> 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.



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 01-23-2013 03:21 PM

Re: ios_base::ate and tellp()
 
On Wed, 23 Jan 2013 06:51:15 -0800, Alex Vinokur wrote:

> On Wednesday, January 23, 2013 4:44:12 PM UTC+2, Melzzzzz wrote:
>> 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.

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


All times are GMT. The time now is 04:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.