473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to use iostream library to copy files

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
4 2637

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: ernst.stiegler | last post by:
Hello, I just want to read a whole line from a console input. What I don't understand is that I always have to press ENTER twice in order to see the line I've entered. Here's my code : ...
12
by: Moritz Steinberger | last post by:
Hello, I want to use a tutorial to take first steps. I use MS Visual c++ 6.0 and Win 98. When I try to compile a "hello world" program using the <iostream.h> library and cout << "hello world",...
2
by: Sigmund Skjelnes | last post by:
Hi! Trying to get a simple cpp program to compile, I'd got the linker complaining it could'nt find eiter cout nor endl or other object which belong to iostream. I'd had to use stdiolib to get the...
9
by: ai | last post by:
Hi , I have an application which has to read files of size few mba nd read them into array . Since i am using STL and std namespace to avoid the error error C2872: 'ifstream' : ambiguous...
2
by: humble04 | last post by:
Hi, I am compiling a collection of C++ code. Most of them are using the new format #include <iostream>. I think all of them because I failed at finding out which header file uses the old format ...
1
by: brian.oneil2 | last post by:
Is there a way to install this onto a network file share and allow a team to access it? I would say share a CD from a networked CD drive, but there are multiple CD's that would have to be inserted....
1
by: lars.uffmann | last post by:
Hello everyone! I just debugged a pretty huge project, eliminating basically every memory leak that would occur with the current configuration files, at least according to the mtrace() tool from...
4
by: Tom Picket | last post by:
Hello, I'm relatively new to C, although I've touched the subject several times for a couple of years. Tutorials etc. always taught me that I should use (in the beginning at least) cin and cout...
19
by: Robert Kochem | last post by:
Hi, I am relative new to C++ regarding it's functions and libraries. I need to access files larger than 4GB which is AFAIK not possible with the STL iostream - at least not if using a 32 bit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.