每天学点C++循环读取文件夹中的文件

  1. //读取文件,将处理结果输出  
  2. void ReadFiles(CString strAP, string _outPath)   
  3. {  
  4.     ifstream read;  
  5.     read.open(strAP);  
  6.     ofstream write;  
  7.     write.open(_outPath.c_str(), ios::out | ios::app);  
  8.   
  9.     if (write.fail() | read.fail())  
  10.     {  
  11.         cout << "ERROR!" << endl;  
  12.     }  
  13.   
  14.     string line;  
  15.   
  16.     while(!read.eof())  
  17.     {  
  18.         getline(read, line);  
  19.           
  20.         if (line.size() == 0 || line =="\n")  
  21.         {  
  22.             continue;  
  23.         }  
  24.   
  25.         write << line << endl;  
  26.     }  
  27.   
  28.     read.close();  
  29.     write.close();  
  30. }  
  31.   
  32. //循环读取每个文件  
  33. void GetAllFiles(CString _strPath, string _outPath)  
  34. {  
  35.     CString strAP;  
  36.   
  37.     CFileFind finder;  
  38.     BOOL bworking=finder.FindFile(_strPath+"//*.*");  
  39.     int i = 0;  
  40.     while(bworking)  
  41.     {  
  42.         bworking=finder.FindNextFile();  
  43.         strAP=finder.GetFilePath();  
  44.   
  45.         cout << "\r" << i++ << " " << strAP;  
  46.   
  47.         if(finder.IsDots())  
  48.         {  
  49.             continue;  
  50.         }  
  51.         if(finder.IsDirectory())  
  52.         {    
  53.             GetAllFiles(strAP, _outPath);  
  54.         }  
  55.           
  56.         ReadFiles(strAP, _outPath);  
  57.     }  
  58.   
  59.     cout << endl;  
  60.     finder.Close();  
  61. }  
  62.   
  63. int _tmain(int argc, _TCHAR* argv[])  
  64. {  
  65.     //遍历的文件夹  
  66.     GetAllFiles("E:\\课题\\语料库\\863样本集Seg\\U""U.txt");  
  67.       
  68.     return 0;  
  69. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值