您的位置:首页 > 编程语言 > C语言/C++

【c++系列】iftream读取文件中每行或者每个单词

2014-03-14 22:08 375 查看

说明

读取每行或者每个单词。

代码

#include <iostream>
#include <fstream>

using namespace std;

int main(){
string filename="F:result.txt";
ifstream ifs(filename.c_str());
if(!ifs){
cerr<<"error: unable to open input file:"<<filename<<endl;
return -1;
}
// string word; //读取每个单词
// while(ifs>>word){
//	cout<<word<<endl;
string line; //读取每行
while(getline(ifs,line)){
cout<<line<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: