dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Intermediate
Aug 29, 2000

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Copying Files with File Streams

To copy the contents of one file into another, use the fstream classes. First, open the source file using an ifstream object, then create a target file using ofstream, and finally, read the source into the target using the rdbuf() member function:

 
#include <ftsream>
int main()
{
 std::ifstream in ("oldfile.txt"); // open original file
 std::ofstream out("newfile.txt"); // open target file
 out << in.rdbuf(); // read original file into target
 out.close(); // explicit close, unnecessary in this case
 in.close();// explicit close, unnecessary in this case
}

Note that you don't have to call the close() member function because the fstream objects do that automatically when their destructors are invoked. However, you want to call close() if other parts of the program access the files before the fstream objects go out of scope.

Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date