-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadall.cpp
More file actions
33 lines (26 loc) · 722 Bytes
/
readall.cpp
File metadata and controls
33 lines (26 loc) · 722 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
#include "readall.h"
#include <iostream>
#include <fstream>
#include <cerrno>
#include <cstring>
long readall(std::string name, std::vector<std::string> &lines) {
std::ifstream inp;
inp.open(name);
if (!inp.good()) {
std::string msg{ "cannot open '" + name + "': " };
msg += strerror(errno);
throw(std::runtime_error(msg));
}
long n{ 0 }; // nr. of lines read
std::string l; // line buffer
while (std::getline(inp,l)) {
lines.push_back(l);
++n;
}
if (!inp.eof()) {
std::string msg { "error while reading '" + name + "': " };
msg += strerror(errno);
throw(std::runtime_error(msg));
}
return n;
}