[英]How do i read from a file C++ and output it
我尝试使用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
读取,而不要使用Karoly在注释中指出的std::ofstream
。
看起来您#include
d <stdio.h>
或<cstdio>
,因此正在尝试使用C的getline
函数。 更改:
getline(myfile,sline);
至:
std::getline(myfile,sline);
确保您使用的是C ++ getline
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.