devc++读取txt文件的写法
时间: 2024-01-05 18:04:02 浏览: 403
在DevC++中读取txt文件的写法如下所示:
```c++
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("E:\\C++\\c++plus 习题\\h.txt", ios::in);
if (!infile) {
cout << "无法打开文件" << endl;
return 1;
}
string line;
while (getline(infile, line)) {
cout << line << endl;
}
infile.close();
return 0;
}
```
这段代码使用了`ifstream`类来打开文件,并使用`open`函数指定文件路径和打开模式。在打开文件之后,我们可以使用`getline`函数逐行读取文件内容,并将每行内容输出到控制台。最后,使用`close`函数关闭文件。
阅读全文
相关推荐













