Re: ostreams, ios::binary, endian, mixed binary-ascii

Discussion in 'C++' started by Marc Schellens, Jul 14, 2003.

  1. Ok, this I got now. Thanks.
    But How I can write out a vector<int> for example?
    Do I need a loop or can it be written out at once?
    thanks again,
    marc

    John Harrison wrote:
    >>>And how can I write some ascii data followed by binary data?

    >>
    >>Just do it.
    >>
    >>
    >>>ios::binary is for the open mode only, isn't it?

    >>
    >>Yes.
    >>

    >
    >
    > I think you don't understand how ios::binary work. << is for 'ascii data'
    > always even when the file is opened in binary mode. write is for binary data
    > always. To mix binary and ascii data just mix << and write.
    >
    > To understand what ios::binary really does, look in your favourite C++ book
    > and read it carefully.
    >
    > john
    >
    >
    Marc Schellens, Jul 14, 2003
    #1
    1. Advertising

  2. And another thing (the documentation on streams I read so far
    is not that clear to me):
    Can I retrieve the filename from a fstream?


    Marc Schellens wrote:
    > Ok, this I got now. Thanks.
    > But How I can write out a vector<int> for example?
    > Do I need a loop or can it be written out at once?
    > thanks again,
    > marc
    >
    > John Harrison wrote:
    >
    >>>> And how can I write some ascii data followed by binary data?
    >>>
    >>>
    >>> Just do it.
    >>>
    >>>
    >>>> ios::binary is for the open mode only, isn't it?
    >>>
    >>>
    >>> Yes.
    >>>

    >>
    >>
    >> I think you don't understand how ios::binary work. << is for 'ascii data'
    >> always even when the file is opened in binary mode. write is for
    >> binary data
    >> always. To mix binary and ascii data just mix << and write.
    >>
    >> To understand what ios::binary really does, look in your favourite C++
    >> book
    >> and read it carefully.
    >>
    >> john
    >>
    >>

    >
    Marc Schellens, Jul 14, 2003
    #2
    1. Advertising

  3. questions...

    My goal is to have an universal class which might be used for input,
    output or both.
    Is there an overhead to use fstreams always?
    Or is it better to use istream for input only etc.
    But in that case my class would have to hold three pointers or objects.

    Marc Schellens wrote:
    > Ok, this I got now. Thanks.
    > But How I can write out a vector<int> for example?
    > Do I need a loop or can it be written out at once?
    > thanks again,
    > marc
    >
    > John Harrison wrote:
    >
    >>>> And how can I write some ascii data followed by binary data?
    >>>
    >>>
    >>> Just do it.
    >>>
    >>>
    >>>> ios::binary is for the open mode only, isn't it?
    >>>
    >>>
    >>> Yes.
    >>>

    >>
    >>
    >> I think you don't understand how ios::binary work. << is for 'ascii data'
    >> always even when the file is opened in binary mode. write is for
    >> binary data
    >> always. To mix binary and ascii data just mix << and write.
    >>
    >> To understand what ios::binary really does, look in your favourite C++
    >> book
    >> and read it carefully.
    >>
    >> john
    >>
    >>

    >
    Marc Schellens, Jul 14, 2003
    #3
  4. >>Ok, this I got now. Thanks.
    >>But How I can write out a vector<int> for example?
    >>Do I need a loop or can it be written out at once?
    >>thanks again,
    >>marc

    >
    >
    > For ascii you need an explicit loop, e.g.
    >
    > for (int i = 0 ; i< vec.size(); ++i)
    > cout << vec << ' ';
    >
    > or an implicit loop
    >
    > std::copy(vec.begin(), vec.end(), ostream_iterator(cout, " "));
    >
    > For binary you can take advantage of the fact that all the elements of the
    > vector are contiguous in memory and just do this
    >
    > file.write(&vec[0], vec.size()*sizeof vec[0]);


    Thanks once more.
    This sounds good, actually I am using a valarray.
    But is this contiguousity guaranteed by the standard?
    If so also for a valarray?
    Marc Schellens, Jul 14, 2003
    #4
  5. >
    > > And how can I write some ascii data followed by binary data?

    >
    > Just do it.
    >
    > > ios::binary is for the open mode only, isn't it?

    >
    > Yes.
    >


    I think you don't understand how ios::binary work. << is for 'ascii data'
    always even when the file is opened in binary mode. write is for binary data
    always. To mix binary and ascii data just mix << and write.

    To understand what ios::binary really does, look in your favourite C++ book
    and read it carefully.

    john
    John Harrison, Jul 15, 2003
    #5
  6. "Marc Schellens" <> wrote in message
    news:...
    > Ok, this I got now. Thanks.
    > But How I can write out a vector<int> for example?
    > Do I need a loop or can it be written out at once?
    > thanks again,
    > marc


    For ascii you need an explicit loop, e.g.

    for (int i = 0 ; i< vec.size(); ++i)
    cout << vec << ' ';

    or an implicit loop

    std::copy(vec.begin(), vec.end(), ostream_iterator(cout, " "));

    For binary you can take advantage of the fact that all the elements of the
    vector are contiguous in memory and just do this

    file.write(&vec[0], vec.size()*sizeof vec[0]);

    john
    John Harrison, Jul 15, 2003
    #6
  7. "Marc Schellens" <> wrote in message
    news:...
    > And another thing (the documentation on streams I read so far
    > is not that clear to me):
    > Can I retrieve the filename from a fstream?
    >


    No, you cannot.

    john
    John Harrison, Jul 15, 2003
    #7
  8. Re: questions...

    "Marc Schellens" <> wrote in message
    news:...
    > My goal is to have an universal class which might be used for input,
    > output or both.
    > Is there an overhead to use fstreams always?
    > Or is it better to use istream for input only etc.
    > But in that case my class would have to hold three pointers or objects.
    >


    I don't see why there should be any overhead although it depends how fstream
    is written of course. fstream is designed so that it can be implemented
    using a single buffer for both input and output.

    john
    John Harrison, Jul 15, 2003
    #8
  9. "Marc Schellens" <> wrote in message
    news:...
    > >>Ok, this I got now. Thanks.
    > >>But How I can write out a vector<int> for example?
    > >>Do I need a loop or can it be written out at once?
    > >>thanks again,
    > >>marc

    > >
    > >
    > > For ascii you need an explicit loop, e.g.
    > >
    > > for (int i = 0 ; i< vec.size(); ++i)
    > > cout << vec << ' ';
    > >
    > > or an implicit loop
    > >
    > > std::copy(vec.begin(), vec.end(), ostream_iterator(cout, " "));
    > >
    > > For binary you can take advantage of the fact that all the elements of

    the
    > > vector are contiguous in memory and just do this
    > >
    > > file.write(&vec[0], vec.size()*sizeof vec[0]);

    >
    > Thanks once more.
    > This sounds good, actually I am using a valarray.
    > But is this contiguousity guaranteed by the standard?


    Complicated answer.

    1) No it isn't
    2) This was probably an oversight
    3) Next revision of standard will correct this
    4) All existing implementations make vector contiguous
    5) Probably impossible to statisfy the requirements of the standard without
    making vector contiguous

    > If so also for a valarray?
    >


    Yes I believe that is guaranteed by the standard.

    john
    John Harrison, Jul 15, 2003
    #9
    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. hicham
    Replies:
    2
    Views:
    9,000
    dxcoder
    Jul 2, 2003
  2. Ernst Murnleitner

    float: IEEE, big endian, little endian

    Ernst Murnleitner, Jan 13, 2004, in forum: C++
    Replies:
    0
    Views:
    845
    Ernst Murnleitner
    Jan 13, 2004
  3. invincible

    Little Endian to Big Endian

    invincible, Jun 14, 2005, in forum: C++
    Replies:
    9
    Views:
    14,318
    Old Wolf
    Jun 14, 2005
  4. invincible
    Replies:
    1
    Views:
    533
    red floyd
    Jun 14, 2005
  5. hicham

    convert from big-endian to little-endian

    hicham, Jun 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,509
    hicham
    Jun 30, 2003
Loading...

Share This Page