forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex8_04.cpp
35 lines (31 loc) · 786 Bytes
/
ex8_04.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
//
// ex8_04.cpp
// Exercise 8.4
//
// Created by pezy on 11/9/14.
//
// @Brief Write a function to open a file for input and read its contents into a vector of strings,
// storing each line as a separate element in the vector.
#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 (std::getline(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;
}