C++时时读取txt文件

  1. /* 
  2.  * File:   readuntil.cpp 
  3.  * Author:yy
  4.  * 
  5.  * Created on 2013年8月30日, 上午10:44 
  6.  */  
  7. #include <iostream>  
  8. #include <fstream>  
  9. #include <string>  
  10. #include <unistd.h>  
  11. #include <ctime>  
  12. #include <cstdlib>  
  13.   
  14. using namespace std;  
  15.   
  16. int main(int argc, char* argv[]) {  
  17.     if (argc != 3){  
  18.         cout<<argv[0]<<" [in_file] [max_time]"<<endl;  
  19.         return 1;  
  20.     }  
  21.   
  22.     time_t max_time = atol(argv[2]);  
  23.   
  24.   
  25.     ifstream ifs;  
  26.     ifs.open(argv[1], ios::in);  
  27.     if (!ifs) {  
  28.         cout<<"open erro"<<endl;  
  29.         return 1;  
  30.     }  
  31.   
  32.     string row;  
  33.     size_t seek;  
  34.     time_t t;  
  35.     do{  
  36.         if (ifs.peek() == EOF) {  
  37.             time(&t);  
  38.             if (t>max_time){  
  39.                 break;  
  40.             }  
  41.             ifs.clear();  
  42.             ifs.seekg(seek, ios::beg);  
  43.             sleep(3);  
  44.             continue;  
  45.         }  
  46.   
  47.         getline(ifs, row);  
  48.         cout<<row<<endl;  
  49.         seek = ifs.tellg();  
  50.     }while(1);  
  51.     ifs.close();  
  52.   
  53.     return 0;  
  54. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值