我尝试使用 getline
但它给了我一些错误
注意:__ssize_t getline(char**, size_t*, FILE*)
我的线路是这样的
ofstream myfile;
myfile.open("file.txt");
while(!myfile.eof())
{
getline(myfile,sline);
cout << sline;
}
我如何让我的 C++ 读取 file.txt
最佳答案
确保您有 #include <string>
, 其中 std::getline()
已定义,并且 sline
是 std::string
.
将循环结构更改为:
while (std::getline(myfile, sline))
{
std::cout << sline << "\n";
}
避免处理失败的读取。
使用 std::ifstream
阅读,而不是std::ofstream
正如 Karoly 在评论中指出的那样。
关于c++ - 我如何从文件 C++ 中读取并输出它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614542/