当前位置: 代码迷 >> 综合 >> C++,ifstream对象调用getline按行读取文本文件
  详细解决方案

C++,ifstream对象调用getline按行读取文本文件

热度:98   发布时间:2024-01-19 15:31:42.0

 

C++ Prime P255  本来是学习该页的用 vector<string> files

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void main()
{
ifstream input;
string s,filename = "c:/ludashi.txt";
vector<string> files;
files.push_back(filename);
vector<string>::const_iterator it = files.begin();
while (it!=files.end()) {
input.open(filename.c_str());
if(!input)break;
while(input>>s)cout<<s<<endl;
input.close();
input.clear();
++it;
}
}


 

 

 

自己捣鼓了一个用getline 读文件:

getline此时的用法 直接 getline(ifile,s)

ifile: 读文件的流,即ifstream对象 

s:  string 对象

// 标准IO库.cpp
//P253
#include <iostream>
#include <fstream>
#include <string>
using namespace std;