ios_base::ate and tellp()

Discussion in 'C++' started by Alex Vinokur, Jan 23, 2013.

  1. Alex Vinokur

    Alex Vinokur Guest

    #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


    > ./a.out


    12
    0


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


    Alex
    Alex Vinokur, Jan 23, 2013
    #1
    1. Advertising

  2. On 1/23/2013 2:26 AM, Alex Vinokur wrote:
    > #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

    >
    >> ./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
    Victor Bazarov, Jan 23, 2013
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

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

    >
    > >

    >
    > >> ./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::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 ?
    Alex Vinokur, Jan 23, 2013
    #3
  4. Alex Vinokur

    Melzzzzz Guest

    On Wed, 23 Jan 2013 06:40:30 -0800, Alex Vinokur wrote:

    >
    > 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.
    Melzzzzz, Jan 23, 2013
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

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



    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.
    Alex Vinokur, Jan 23, 2013
    #5
  6. Alex Vinokur

    Melzzzzz Guest

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

    >
    >
    > 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.
    Melzzzzz, Jan 23, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandor
    Replies:
    0
    Views:
    596
    Sandor
    Apr 25, 2005
  2. Geoffrey S. Knauth
    Replies:
    6
    Views:
    980
    Earl Purple
    Jan 18, 2006
  3. DaVinci
    Replies:
    3
    Views:
    419
    Jacek Dziedzic
    Apr 9, 2006
  4. DaVinci
    Replies:
    7
    Views:
    709
    Fraser Ross
    Apr 7, 2006
  5. Robin
    Replies:
    9
    Views:
    1,238
Loading...

Share This Page