Skip to content

Commit 70fb251

Browse files
authored
Update RAII.md
1 parent 03ee450 commit 70fb251

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

codingStyleIdioms/3_RAII/RAII.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ vector<string> read_lines_from_file(string &file_name) {
6666
string line;
6767

6868
ifstream file_handle (file_name.c_str());
69-
while (file_handle.good() && !file_handle.eof()) {
69+
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
7070
getline(file_handle, line);
7171
lines.push_back(line);
7272
}
@@ -89,7 +89,7 @@ int main(int argc, char* argv[]) {
8989
输出:
9090
9191
```cpp
92-
File makefile contains 38 lines.
92+
File makefile contains 37 lines.
9393
```
9494

9595
这看起来很简单。`vector`被填满、返回和调用。然而,作为关心性能的高效程序员,这方面的一些问题困扰着我们:在return语句中,由于使用了值语义,`vector`在销毁之前不久就被复制到一个新`vector`中。
@@ -104,7 +104,7 @@ vector<string> * read_lines_from_file(string &file_name) {
104104
string line;
105105

106106
ifstream file_handle (file_name.c_str());
107-
while (file_handle.good() && !file_handle.eof()) {
107+
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
108108
getline(file_handle, line);
109109
lines->push_back(line);
110110
}
@@ -147,7 +147,7 @@ vector<string> * read_lines_from_file(string &file_name) {
147147
string line;
148148

149149
ifstream file_handle (file_name.c_str());
150-
while (file_handle.good() && !file_handle.eof()) {
150+
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
151151
getline(file_handle, line);
152152
lines->push_back(line);
153153
}
@@ -265,7 +265,7 @@ unique_ptr<vector<string>> read_lines_from_file(string &file_name) {
265265
string line;
266266
267267
ifstream file_handle (file_name.c_str());
268-
while (file_handle.good() && !file_handle.eof()) {
268+
while (file_handle.good() && !file_handle.eof() && file_handle.peek()!=EOF) {
269269
getline(file_handle, line);
270270
lines->push_back(line);
271271
}
@@ -309,4 +309,4 @@ RAII代表“资源获取是初始化”。
309309

310310
> 1.https://www.toptal.com/software/eliminating-garbage-collector#remote-developer-job
311311
312-
> 2.https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii
312+
> 2.https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii

0 commit comments

Comments
 (0)