By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,975 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,975 IT Pros & Developers. It's quick & easy.

How to use iostream library to copy files

P: n/a
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 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

<He************@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
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::ofstream' 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::ofstream' are declared by
standard header <fstream>.

-Mike
Jul 6 '07 #2

P: n/a
On Jul 6, 5:58 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
<Herman.Schu...@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
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:ja*********@gmail.com
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

Jul 6 '07 #3

P: n/a

"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
On Jul 6, 5:58 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
<Herman.Schu...@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
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
Jul 6 '07 #4

P: n/a
On Jul 6, 10:01 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"James Kanze" <james.ka...@gmail.comwrote 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: ja*********@gmail.com
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

Jul 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.