C++ 读取stdio.h时的无限循环
为什么下面的程序会陷入无限循环中C++ 读取stdio.h时的无限循环,c++,C++,为什么下面的程序会陷入无限循环中 int main() { string fname = "C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\include\\stdio.h"; char line[985]; ifstream file(fname.c_str()); if(file == NULL) { cout<<"unable to open";
int main()
{
string fname = "C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\include\\stdio.h";
char line[985];
ifstream file(fname.c_str());
if(file == NULL)
{
cout<<"unable to open";
exit(0);
}
while(!file.eof())
{
file.getline(line,'\n');
cout<<line<<'\n';
}
}
intmain()
{
string fname=“C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\include\\stdio.h”;
字符行[985];
ifstream文件(fname.c_str());
if(file==NULL)
{
coutgetline()的第二个参数是流大小,第三个是分隔符
与此相反:
file.getline(line,'\n');
试试这个:
file.getline(line,256,'\n');
嗯,我试过了,但是没有用。而且最长的行的长度是282个字符,所以我尝试将“行”的大小设置为所需长度的3倍以上,但仍然不起作用。您不必担心最长行的长度。getline()
读取指定的size
或直到它到达分隔符。我在Linux上运行了您的代码,路径是stdio.h。它打印正确。stdio.h与此问题无关