getline() 的原型/库是什么;

What's the prototype/library for getline();

本文关键字:是什么 原型 getline      更新时间:2023-10-16

我想使用getline((;函数。我把#include放在了标题处。但我得到了一个错误,说"函数‘getline’应该有一个原型"。图书馆错了吗?或者我需要另一个图书馆?这是我的代码

#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<iostream.h>
void main(){
char word;
ofstream file;
file.open("code.txt");
file.close();
while(getline(file,word))
{
cout<<word<<"n";
}
getch();
}

您的问题是getline()适用于getline(ifstream, string)而不是getline(ofstream, char)。因此,您的代码应该更像这样:

string word;
ifstream myFile("code.txt");
while(getline(myFile,word))
cout<< word<< "n";