我试图用函数getline如何从一个文件C++和输出阅读
,但它给我一些错误
注:__ssize_t函数getline(字符**,为size_t *,FILE *)
我行是这样
ofstream myfile;
myfile.open("file.txt");
while(!myfile.eof())
{
getline(myfile,sline);
cout << sline;
}
如何让我的C++读取file.txt的
我试图用函数getline如何从一个文件C++和输出阅读
,但它给我一些错误
注:__ssize_t函数getline(字符**,为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
。
它看起来像你#include
D <stdio.h>
或<cstdio>
,因此试图使用C的getline
函数。变化:
getline(myfile,sline);
到:
std::getline(myfile,sline);
,以确保您使用的是C++ getline
。
谢谢你在那里。 – 2012-07-23 14:33:40
+1提及名称冲突。 – hmjd 2012-07-23 14:37:19
'ofstream' .. ?? – 2012-07-23 14:30:23
您没有发布实际的错误消息。然后......是的,Karoly说了什么。 – 2012-07-23 14:30:52
使用'ifstream'来读取。 'ofstream'是为了写作。 – Erik 2012-07-23 14:31:00