把文本文件一行一行读出来

来源:互联网 发布:微信报修系统源码 编辑:程序博客网 时间:2024/04/29 13:45

 //把文本文件一行一行读出来,存放到向量vec中
//下面程序在bcb下通过
//如果是在vc下运行,需要把
// txt.push_bac(ch) 改为  txt+=ch;
// txt.clear()  改为 txt="";
#include<string>
#include<fstream>
#include<vector>
void readfile(string filename,vector<string>& vec)
{
   ifstream ifile;
   ifile.open(filename.c_str());
   if(ifile.fail())
      return;

   string txt;
   char ch;
   while(ifile.get(ch)){
       if(ch=='/n'){
           vec.push_back(txt);
           txt.clear();
       }else
           txt.push_back(ch);
   }
   if(ch!='/n')       //添加最后一行
     vec.push_back(txt);
}

原创粉丝点击