a file of ints

Hi, the question is : in what file format should i store a bunch of ints
in order to be able to retrieve them using a std::istream_iterator one int at a time ?
You can simply use a .txt file for the numbers. Then the code could be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile ("numbers.txt");
    vector<int> vec;

    istream_iterator<int> iter(inFile) ;
    istream_iterator<int> eof;

    while (iter != eof) {
        vec.push_back(*iter++);
    }

    inFile.close();

    for (unsigned int i=0; i<vec.size(); i++) {
        cout << vec[i] << endl;
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.