Home arrow C++ arrow Page 3 - 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++ - Reading Inside a Container
(Page 3 of 4 )

You can use the same trick in the other direction (input), since it's just as easy. Let us suppose you have the input waiting for input in a predefined way. For example, we will try with a sequence of int types. Now we will copy in the other direction, so we need to indicate from where to copy, and to where. The first two members are the istream_iterator that will delimit the borders. When we pass a class to it, it will return an iterator to its start, and no parameter will just point to the end of file.

The last argument is a little more interesting. Under normal conditions, whenever you make a copy, you make sure that the target is large enough to hold the incoming stream of data. This is achieved by calling the destination resize() function, but in the case of this iterator type, this process is simplified by calling the push_back() method on the destination iterator. Hence, a vector can handle any size as long as you have enough physical and virtual memory available.


#include <iostream>

#include <vector>

#include <algorithm>

#include <fstream>

using namespace std;


int main()

{

vector<int> numbers;

 

ifstream in;

in.open("In.txt");

if (!in)

{

return 0; // terminate program, nothing more to do

}


copy( istream_iterator<int>(in), istream_iterator<int>(), back_insert_iterator<vector<int>>(numbers));


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

 

return 1;

}


The input file contained the following:


97 98 122 100 101A 12


With the output:


a...

b...

z...

d...

e...

Press any key to continue . . .


The rule stands. If you have defined a special class, and if you define the extraction function (operator >>) for it, you will be able to call it for this kind of input. For each member, the extraction function will be called to resolve the input. Naturally, you need to call the iterator creations to those kinds of template arguments.


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