getline(istream &in, string &s)
从输入流读入一行到string s
?功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
?读入了文件结束标志
?读到一个新行
?达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string buff; ifstream infile; ofstream outfile; cout<<"Input file name: "<<endl; cin>>buff; infile.open(buff.c_str()); if(!infile) cout<<"error"<<buff<<endl; cout<<"Input outfile name: "<<endl; cin>>buff; outfile.open(buff.c_str()); while(getline(infile, buff)) outfile<<buff<<endl; infile.close(); outfile.close(); return 0; }