How to use iostream library to copy files

H

Herman.Schultz

Hi,

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

Thank you.
 
M

Mike Wahler

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
 
J

James Kanze

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

Mike Wahler

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
 
J

James Kanze

"James Kanze" <[email protected]> wrote in message

[...]
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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top