Home arrow C++ arrow Page 2 - Advanced File Handling with Streams in C++
C++

Advanced File Handling with Streams in C++


In the previous article of this series, titled "File Handling and Streams in C++," you saw how to handle files in an easy and convenient way. Getting input data from a file or putting such data into a file should be familiar to you now. In this article, we'll take a look at some more advanced tasks and tricks so you can gain speed and efficiency in your work.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
April 14, 2009
TABLE OF CONTENTS:
  1. · Advanced File Handling with Streams in C++
  2. · Printing from Containers
  3. · Reading Inside a Container
  4. · Writing to Two Streams

print this article
SEARCH DEVARTICLES

Advanced File Handling with Streams in C++ - Printing from Containers
(Page 2 of 4 )

Probably from the first day that you started learning the C++ language, you've heard that you should start using the STL library, as it offers great containers. If you initially started with the C programming classes and you like arrays, I urge you also to get used to the vector container at least.

Often, you have a collection of types in a vector and you want to print them to the console (or a file) separated by a space, or every item in new line. Writing this can be done in just a line:


#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;


int main()

{

std::vector<int> numbers;

numbers.push_back(97);

numbers.push_back(98);

numbers.push_back(99);

 

copy(numbers.begin(), numbers.end(), ostream_iterator<char>(cout, "...n"));


 

return 1;

}


We use the copy algorithm; it copies from our container to the stream. The real strength of this design is the ostream_class and its parameters. The class returns an iterator that will point out where to print the content between the first two iterators of the copy. In addition, the ostream_iterator class is highly customizable, with inputs like output format, where to put it, and what delimiter will be used between the members.

The template argument must be one for which the insertion operator is declared. As long as all of this is completed, you can call this method even for your own custom class, as the operator<< will be used to copy the streams from the container to the output stream. The result in the upper case looks impressive; we just converted the int values into their ASCI chars.


a...

b...

c...

Press any key to continue . . .



blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials