forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex8_05.cpp
More file actions
38 lines (34 loc) · 739 Bytes
/
Copy pathex8_05.cpp
File metadata and controls
38 lines (34 loc) · 739 Bytes
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
//
// ex8_05.cpp
// Exercise 8.5
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Rewrite the previous program to store each word in a separate
// element.
// @See ex8_04.cpp
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using std::vector;
using std::string;
using std::ifstream;
using std::cout;
using std::endl;
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs) {
string buf;
while (ifs >> buf) vec.push_back(buf);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("../data/book.txt", vec);
for (const auto& str : vec) cout << str << endl;
return 0;
}