天天看点

C++中多次循环输出一个文件内的每行信息

#include <iostream>
#include <string.h>
using namespace std;
extern int x;


int main()
{

    FILE *_test_fp;
    char record[];
    int cur_readtimes =;
    int looptimes = ;
    _test_fp = fopen("/Desktop/test/1.txt", "r");
    while(true){
        if (fgets(record, sizeof(record), _test_fp)){
            cout<<record<<endl;
        }else{
             if(cur_readtimes<looptimes){//只有读完一次文件的时候才会重新打开这个文件
                                   cur_readtimes++;
                                   fclose(_test_fp);
                                   _test_fp = fopen("/Desktop/test/1.txt", "r");//再次打开那个文件

                                   cout<<" now start h read file, total loopd";
                                   continue;

                            }
        }
    }

      fclose(_test_fp);

}