C++ getline的使用

时间:2014-02-25 12:46:51   收藏:0   阅读:346

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;

}


原文:http://blog.csdn.net/slience_perseverance/article/details/19819601

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!