-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx5_9.cpp
45 lines (42 loc) · 1016 Bytes
/
Ex5_9.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Created by kwoodle on 1/31/19.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
//
// X.5 [9] (∗1) Open a file for writing (as an ofstream ) and write a few hundred integers to it.
// [10] (∗1) Open the file of integers from §X.5[9] for reading (as an ifstream ) and read it.
//
int main() {
ofstream ofile{"test.txt", ios_base::trunc};
for (int i = 1; i < 250; ++i) {
ofile << i << " ";
if (i % 20 == 19)
ofile << "\n";
}
ofile.close();
ifstream ifile{"test.txt"};
if (!ifile.is_open())
return 1;
string line;
vector<int> Ints;
while (std::getline(ifile, line)) {
istringstream ss(line);
int I;
while (ss >> I) {
Ints.push_back(I);
}
}
cout << "\nContents of file\n";
int ii = 0;
for (auto e:Ints) {
cout << e << ", ";
++ii;
if (ii % 20 == 19)
cout << "\n";
}
return 0;
}