我尝试使用 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>, wherestd::getline()被定义,那sline是一个std::string.
将循环结构更改为:
while (std::getline(myfile, sline))
{
std::cout << sline << "\n";
}
以避免处理失败的读取。
std::ifstream用来阅读,不像卡罗利std::ofstream在评论中指出的那样。
看起来你#included<stdio.h>或<cstdio>因此正在尝试使用 C 的getline函数。改变:
getline(myfile,sline);
到:
std::getline(myfile,sline);
以确保您使用的是 C++ getline。