Skip to content

Commit

Permalink
Update c++_example1.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-City committed Apr 10, 2022
1 parent 92dd0fe commit 03ee450
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions codingStyleIdioms/3_RAII/c++_example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ vector<string> read_lines_from_file(string &file_name) {
string line;

ifstream file_handle (file_name.c_str());
while (file_handle.good() && !file_handle.eof()) {
// file_handle.peek()!=EOF 解决多读一行问题
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
getline(file_handle, line);
lines.push_back(line);
}
Expand All @@ -26,7 +27,8 @@ vector<string> * read_lines_from_file1(string &file_name) {
string line;

ifstream file_handle (file_name.c_str());
while (file_handle.good() && !file_handle.eof()) {
// file_handle.peek()!=EOF
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
getline(file_handle, line);
lines->push_back(line);
}
Expand All @@ -40,7 +42,7 @@ vector<string> * read_lines_from_file1_1(string &file_name) {
string line;

ifstream file_handle (file_name.c_str());
while (file_handle.good() && !file_handle.eof()) {
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
getline(file_handle, line);
lines->push_back(line);
}
Expand All @@ -63,4 +65,4 @@ int main() {
int count1 = read_lines_from_file1_1(file_name1)->size();
cout << "File " << file_name << " contains " << count1 << " lines.";
return 0;
}
}

0 comments on commit 03ee450

Please sign in to comment.