file io question

Discussion in 'C++' started by Fluke, May 22, 2004.

  1. Fluke

    Fluke Guest

    I'm looking for a way to copy a segment of a binary file.
    ie. from start position (>=0) to end position (<= eof).
    Any suggestions? (preferably stl)
    Fluke, May 22, 2004
    #1
    1. Advertising

  2. "Fluke" <> wrote in message
    news:IQLrc.104656$...
    > I'm looking for a way to copy a segment of a binary file.
    > ie. from start position (>=0) to end position (<= eof).
    > Any suggestions? (preferably stl)
    >


    void copy_segment(istream& input, streampos start, streampos end,
    vector<char>& output)
    {
    input.seekg(start);
    output.resize(end - start);
    input.read(&output[0], end - start);
    }

    Untested code, and obviously could do with the addition of some error
    checking.

    john
    John Harrison, May 22, 2004
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message
    news:...
    >
    > "Fluke" <> wrote in message
    > news:IQLrc.104656$...
    > > I'm looking for a way to copy a segment of a binary file.
    > > ie. from start position (>=0) to end position (<= eof).
    > > Any suggestions? (preferably stl)
    > >

    >
    > void copy_segment(istream& input, streampos start, streampos end,
    > vector<char>& output)
    > {
    > input.seekg(start);
    > output.resize(end - start);
    > input.read(&output[0], end - start);
    > }
    >
    > Untested code, and obviously could do with the addition of some error
    > checking.
    >
    > john


    bool copy_segment(istream& input, streampos start, streampos end,
    vector<char>& output)
    {
    vector<char> temp(end - start);
    if (!input.seekg(start) || !input.read(&temp[0], end - start))
    return false;
    output.swap(temp);
    return true;
    }

    Still untested.

    john
    John Harrison, May 23, 2004
    #3
    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. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,404
    Scott Allen
    Jun 4, 2005
  2. key9
    Replies:
    7
    Views:
    374
    Pete Becker
    Sep 13, 2006
  3. Dave
    Replies:
    1
    Views:
    990
    Juan T. Llibre
    Jun 8, 2007
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    786
    Alf P. Steinbach
    Jul 10, 2007
  5. scad
    Replies:
    4
    Views:
    948
    James Kanze
    May 28, 2009
Loading...

Share This Page