How to use iostream library to copy files

Discussion in 'C++' started by Herman.Schultz@gmail.com, Jul 6, 2007.

  1. Guest

    Hi,

    How can I use iostream library in c++ to copy a file from i th byte
    to an output file?

    Thank you.
     
    , Jul 6, 2007
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > How can I use iostream library in c++ to copy a file from i th byte
    > to an output file?


    Create a 'std::ifstream' object using constructor
    parameters of your input file name and binary mode.

    Check the stream state to ensure it successfully opened.

    Create a 'std::eek:fstream' object using constructor
    parameters of your output file name and binary mode.

    Check the stream state to ensure it successfully opened.

    Read characters from the input stream (e.g. using the
    'get()' member function), counting how many characters
    have been read, until you reach the 'ith'.

    Read a character from the input stream.
    Write that character to the output stream (e.g. with the
    'put()' member function).

    After every read and write operation, check the stream
    state to determine if the operation succeeded. When
    the input stream indicates a fail state, either there
    was an error, or you've reached end of file (determine
    the difference with the 'eof()' member function.)

    Close both files (or cause them to be automatically closed
    by exiting the scope where they're defined.

    The 'std::ifstream' and 'std::eek:fstream' are declared by
    standard header <fstream>.

    -Mike
     
    Mike Wahler, Jul 6, 2007
    #2
    1. Advertising

  3. James Kanze Guest

    On Jul 6, 5:58 am, "Mike Wahler" <> wrote:
    > <> wrote in message
    > news:...
    > > How can I use iostream library in c++ to copy a file from i th byte
    > > to an output file?


    Globally, the answer is almost 100% correct. (Especially the
    fact that you remind to use binary mode, and to check for
    errors.) Just a few small details.

    [...]
    > Read characters from the input stream (e.g. using the
    > 'get()' member function), counting how many characters
    > have been read, until you reach the 'ith'.


    This can be done using
    input.ignore( i ) ;

    > Read a character from the input stream.
    > Write that character to the output stream (e.g. with the
    > 'put()' member function).


    And the copy itself can be done using:
    output << input.rdbuf() ;

    > After every read and write operation, check the stream
    > state to determine if the operation succeeded. When
    > the input stream indicates a fail state, either there
    > was an error, or you've reached end of file (determine
    > the difference with the 'eof()' member function.)


    > Close both files (or cause them to be automatically closed
    > by exiting the scope where they're defined.


    The one "error". You have to check for errors in the output
    file after closing (or at least after a final flush). Which
    means that you can't simply count on the ofstream object going
    out of scope to close the file.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 6, 2007
    #3
  4. Mike Wahler Guest

    "James Kanze" <> wrote in message
    news:...
    On Jul 6, 5:58 am, "Mike Wahler" <> wrote:
    > <> wrote in message
    > news:...
    > > How can I use iostream library in c++ to copy a file from i th byte
    > > to an output file?


    > Globally, the answer is almost 100% correct.


    Well, if I were perfect, life wouldn't be much fun. :)

    > (Especially the
    > fact that you remind to use binary mode, and to check for
    > errors.) Just a few small details.


    [...]
    > Read characters from the input stream (e.g. using the
    > 'get()' member function), counting how many characters
    > have been read, until you reach the 'ith'.


    > This can be done using
    > input.ignore( i ) ;


    Good idea.

    > Read a character from the input stream.
    > Write that character to the output stream (e.g. with the
    > 'put()' member function).


    > And the copy itself can be done using:
    > output << input.rdbuf() ;


    I thought using separate 'read' and 'write' operations
    would be more suitable for a novice to understand.

    > After every read and write operation, check the stream
    > state to determine if the operation succeeded. When
    > the input stream indicates a fail state, either there
    > was an error, or you've reached end of file (determine
    > the difference with the 'eof()' member function.)


    > Close both files (or cause them to be automatically closed
    > by exiting the scope where they're defined.


    > The one "error". You have to check for errors in the output
    > file after closing (or at least after a final flush). Which
    > means that you can't simply count on the ofstream object going
    > out of scope to close the file.


    I seem to often forget to warn about that. Thanks for pointing
    it out.

    -Mike
     
    Mike Wahler, Jul 6, 2007
    #4
  5. James Kanze Guest

    On Jul 6, 10:01 pm, "Mike Wahler" <> wrote:
    > "James Kanze" <> wrote in message


    [...]
    > > Read a character from the input stream.
    > > Write that character to the output stream (e.g. with the
    > > 'put()' member function).
    > > And the copy itself can be done using:
    > > output << input.rdbuf() ;


    > I thought using separate 'read' and 'write' operations
    > would be more suitable for a novice to understand.


    Definitly. I almost added a warning that this is a subtle and
    not well known feature of iostream. My comments (except
    concerning the fact that you have to check after close on write)
    should be considered "in addition to", and not as a replacement
    for your original text. I would not recommend using functions
    like the above until you were capable of doing it "by hand".

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 7, 2007
    #5
    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. John Tiger
    Replies:
    10
    Views:
    5,602
  2. ai@work
    Replies:
    9
    Views:
    546
    Ron Natalie
    Dec 16, 2004
  3. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    777
    red floyd
    Jan 15, 2005
  4. red floyd
    Replies:
    3
    Views:
    537
    Dietmar Kuehl
    Mar 8, 2005
  5. Alex
    Replies:
    2
    Views:
    1,238
Loading...

Share This Page