ifcplusplus读取文件
时间: 2023-09-30 13:04:18 浏览: 34
如果你想在 C++ 中读取文件,可以使用fstream库。以下是一个简单的例子:
```c++
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream file("example.txt");
std::string line;
if (file.is_open()) {
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
} else {
std::cout << "Unable to open file" << std::endl;
}
return 0;
}
```
在上面的例子中,我们打开了名为 "example.txt" 的文件,使用while循环逐行读取文件内容,并将每行打印到控制台中。当读取完成后,我们关闭了文件。
请注意,使用fstream库需要包含<fstream>头文件,同时使用std命名空间。