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.
Next: Writing to Two Streams >>
More C++ Articles
More By Gabor Bernat