forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex8_04.cpp
38 lines (34 loc) · 814 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
36
37
38
//
// ex8_04.cpp
// Exercise 8.4
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @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;
}