文件拆分(自用)

按行

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#define MAX_LINE_PER_FILE   8  //1000000

int main (int argc, char** argv)
{
    unsigned int num_line = 0;
    int index = 0;

    ofstream out;
    string line;
    string file_out;

    if(argc <= 1){
      cout << "need file input" << endl;
      return 0;
    }

    string file_in =  argv[1];
    ifstream in(file_in, ios::in);

    if(in) {
      while (getline (in, line))
      {
        if ((num_line % MAX_LINE_PER_FILE) == 0){
          if(file_out.length() > 0){
            out.close();
          }

          file_out = "out_" + to_string(index) + ".txt";
          index++;

          out.open(file_out, ios::out);

          if(!out) {
            cout << "file created faild!" << endl;
          }
        }

        out << line << endl;
        num_line++;
      }

      cout << num_line << endl;
      out.close();
    }
    else {
      cout <<"no such file" << endl;
    }
  return 0;
}

按大小

#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
using namespace std;

#if 1
#define MAX_LINE_PER_FILE       100000000
#define SIZE_PER_READ           1000000
#else
#define MAX_LINE_PER_FILE       2000
#define SIZE_PER_READ           1000
#endif


char read_buf[SIZE_PER_READ] = {};

int main (int argc, char** argv)
{
    unsigned int num = 0;
    int index = 0;

    ofstream out;
    string line;
    string file_out;

    if(argc <= 1){
      cout << "need file input" << endl;
      return 0;
    }

    string file_in =  argv[1];
    ifstream in(file_in, ios::binary);

    in.seekg(0, ios::end);
    unsigned int in_size = in.tellg();
    in.seekg(0, ios::beg);

    unsigned int read_size = 0;

    if(in) {
      while (read_size < in_size)
      {
        if ((read_size % MAX_LINE_PER_FILE) == 0){
          if(file_out.length() > 0){
            out.close();
          }

          file_out = "out_" + to_string(index) + ".txt";
          index++;

          cout << file_out << endl;
          out.open(file_out, ios::binary);
          if(!out) {
            cout << "file created faild!" << endl;
          }
        }

        memset(read_buf, 0, SIZE_PER_READ);
        in.read(read_buf, SIZE_PER_READ);
        read_size += SIZE_PER_READ;

        out << read_buf;
      }

      out.close();
    }
    else {
        cout <<"no such file" << endl;
    }
  return 0;
}

折腾 split

#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <sys/time.h>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <queue>
#include <list>
#include <thread>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
using namespace std;

string ver = "20221215 v1.0";
#define MAX_LINE_PER_FILE       0X7FFFFFF
#define SIZE_PER_READ           0X800000    // 0X100000
char p_local_time_begin[128] = {};
char p_local_time_end[128] = {};
char p_local_time_mid[128] = {};
clock_t clock_start;
clock_t clock_finish;
clock_t clock_middle;

#ifdef WIN32
#ifndef _TM_DEFINED
struct tm {
    int tm_sec; /* 秒 – 取值区间为[0,59] */
    int tm_min; /* 分 - 取值区间为[0,59] */
    int tm_hour; /* 时 - 取值区间为[0,23] */
    int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
    int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
    int tm_year; /* 年份,其值等于实际年份减去1900 */
    int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
    int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
    int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
#define _TM_DEFINED
#endif

int gettimeofday(struct timeval *tp, void *tzp)
{
  time_t clock;
  struct tm tm;
  SYSTEMTIME wtm;
  GetLocalTime(&wtm);
  tm.tm_year   = wtm.wYear - 1900;
  tm.tm_mon   = wtm.wMonth - 1;
  tm.tm_mday   = wtm.wDay;
  tm.tm_hour   = wtm.wHour;
  tm.tm_min   = wtm.wMinute;
  tm.tm_sec   = wtm.wSecond;
  tm. tm_isdst  = -1;
  clock = mktime(&tm);
  tp->tv_sec = clock;
  tp->tv_usec = wtm.wMilliseconds * 1000;
  return (0);
}
#endif

void get_local_time(char *time_str, int len, struct timeval *tv)
{
  struct tm* ptm;
  char time_string[800];
  long milliseconds;
  const time_t tt = tv->tv_sec;

  ptm = localtime(&tt);

  // time_t t1;
  // t1 = time(NULL);
  // ptm = localtime(&t1);
  strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);

  milliseconds = tv->tv_usec/1000;
  snprintf (time_str, len, "%s.%03ld", time_string, milliseconds);
  return;
}

void getStartTime()
{
  timeval current_time_tmp;
  gettimeofday(&current_time_tmp, NULL);
  get_local_time(p_local_time_begin, sizeof(p_local_time_begin), &current_time_tmp);
  clock_start = clock();
}

void getMiddleTime()
{
  timeval current_time_tmp;
  gettimeofday(&current_time_tmp, NULL);
  get_local_time(p_local_time_mid, sizeof(p_local_time_mid), &current_time_tmp);
  clock_middle = clock();
}

void getFinishTime()
{
  timeval current_time_tmp;
  gettimeofday(&current_time_tmp, NULL);
  get_local_time(p_local_time_end, sizeof(p_local_time_end), &current_time_tmp);
  clock_finish = clock();
}

void printTime()
{
  double programTimes = ((double) clock_finish - clock_start) / CLOCKS_PER_SEC;
  double middleTimes = ((double) clock_middle - clock_start) / CLOCKS_PER_SEC;
  printf("start  time = %s\n", p_local_time_begin);
  printf("middle time = %s\n", p_local_time_mid);
  printf("finish time = %s\n", p_local_time_end);
  printf("diff  time1 = %0.3f\n", middleTimes);
  printf("diff  time2 = %0.3f\n", programTimes);
}

list<pair<queue<char *>, bool>> que_list;
bool isReadFinish = false;
int read_cnt = 0;
int write_cnt = 0;

void readFunc(ifstream & in)
{
  in.seekg(0, ios::end);
  unsigned int in_size = in.tellg();
  in.seekg(0, ios::beg);

  unsigned int read_size_all = 0;
  queue<char *> data_que;
  while (read_size_all < in_size) {
    if ((read_size_all & MAX_LINE_PER_FILE) == 0){
      if (read_cnt>0) {
        que_list.back().second = true;
        que_list.emplace_back(data_que, false);

        data_que = queue<char *>();
      }
      read_cnt++;
      while (que_list.size() >= 3) {
        sleep(1);
      }
    }

    char * read_buf = (char *)calloc(SIZE_PER_READ, sizeof(char));
    if(!read_buf) {
      cout << "buf malloc failed!" << endl;
      return;
    }

    unsigned int left_size = in_size - read_size_all;
    if(left_size >= SIZE_PER_READ){
      in.read(read_buf, SIZE_PER_READ);
      read_size_all += SIZE_PER_READ;
    } else {
      in.read(read_buf, left_size);
      read_size_all += left_size;
    }

    data_que.emplace(read_buf);
  }

  que_list.back().second = true;
  que_list.emplace_back(data_que, true);
  isReadFinish = true;
  getMiddleTime();
  return;
}

int checkValidLen(char* buf, int size)
{
  int index = size-1;
  for(; index>=0; index--){
    if(buf[index] != 0){
      break;
    }
  }
  return index+1;
}

void writeFile(list<pair<queue<char *>, bool>>::iterator  it)
{
  string file_out;
  ofstream out;
  static int index = 0;
  bool isFull = false;

  if(it == que_list.end()){
    cout << "it == end\n";
    return;
  }

  queue<char *> data_que = (*it).first;
  if(data_que.empty()){
    cout << "empty\n";
    return;
  }

  file_out = "out_" + to_string(index) + ".txt";
  index++;
  out.open(file_out, ios::out|ios::binary);
  if(!out) {
    cout << "file created faild!" << endl;
    return;
  }

  static unsigned int cnt = 0;
  do {
    isFull = (*it).second;
    while(!data_que.empty()) {
      char *buf = data_que.front();
      int len = checkValidLen(buf, SIZE_PER_READ);
      out.write(buf, len);
      free(buf);
      buf = NULL;
      data_que.pop();

      cnt++;
      if ((cnt & 0X7) == 0) {     // 0X3F
        cout << "<";
      }
    }

    usleep(100000);
  } while(!isFull);

  out.close();
  write_cnt++;
  return;
}

void writeFunc()
{
  list<pair<queue<char *>, bool>>::iterator it;
  while(!isReadFinish) {
    if(que_list.size()>0){
      it = que_list.begin();
      writeFile(it);
      que_list.erase(it);
    }
    usleep(100000);
  }

  if (write_cnt < read_cnt) {
    for(it = que_list.begin(); it != que_list.end(); it++) {
      writeFile(it);
      que_list.erase(it);
    }
  }
  return;
}

int main (int argc, char** argv)
{
  cout << "version: " << ver << endl;
  getStartTime();

  if (argc <= 1) {
    cout << "need file input" << endl;
    return 0;
  }

  string file_in =  argv[1];
  ifstream in(file_in, ios::in|ios::binary);

  if(in) {
    thread th_read(readFunc, ref(in));
    thread th_write(writeFunc);

    th_read.join();
    th_write.join();
  } else {
    cout <<"no such file" << endl;
  }

  cout << endl;
  getFinishTime();
  printTime();
  return 0;
}

提取

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;

#if 1
#define MAX_LINE_PER_FILE   1000000
#else
#define MAX_LINE_PER_FILE   8
#endif


int main (int argc, char** argv)
{
    unsigned int num_line = 0;
    unsigned int num_read = 0;
    int index = 0;

    ofstream out;
    string line;
    string file_out;
    bool isNeedDebug = false;

    char str_d[15] = " D/";

    char * str_match = NULL;


    if(argc <= 1){
      cout << "need file input" << endl;
      return 0;
    }

    if(argc <= 2) {
      cout << "need special string input" << endl;
      return 0;
    }

    str_match = argv[2];
    cout << str_match << endl;

    if(argc == 4) {
      if((*argv[3] == 'd') || (*argv[3] == 'D')){
        isNeedDebug = true;
        cout << "need d" << endl;
      }
    }

    string file_in =  argv[1];
    ifstream in(file_in, ios::in);

    if(in) {
      while (getline (in, line))
      {
        num_read++;
        //if (line.find(tvp1) != string::npos)) {
        if(strstr(line.c_str(), str_match) != NULL) {
          if(isNeedDebug == false){
            if(strstr(line.c_str(), str_d) != NULL){
              continue;
            }
          }

          if ((num_line % MAX_LINE_PER_FILE) == 0){
            if(file_out.length() > 0){
              out.close();
            }

            if(isNeedDebug) {
              file_out = "out_d_" + to_string(index) + ".txt";
            }else{
              file_out = "out_" + to_string(index) + ".txt";
            }

            index++;

            out.open(file_out, ios::out);

            if(!out) {
              cout << "file created faild!" << endl;
            }
          }

          out << line << endl;

          num_line++;
        }
      }

      cout << num_read << endl;
      cout << num_line << endl;

      out.close();
    }
    else {
      cout <<"no such file" << endl;
    }
  return 0;
}

特殊字符

   32.945 E/NAGSAM  (P  694, T  694): DebugOverwrite.cpp: m_LoadSettings(175) > [31mFailed to open file: /opt/usr/apps/org.tizen.nagsam/debug.xml error: ????=???Y????[0m
   or: ????=?????????[0m
   203.701 I/SMART_DEADLOCK(P  929, T  933): smart-deadlock-logger.c: write_on_hyperuart(158) > ==00002???Fà$??Mà???????à?MàT????à°??????à°??]  
  
  9190.539 D/TV_PLAYER(P  696, T  822): : tv_stream_priv_activate_audio_pipeline(568) > call begin
  9190.539 E/TV_PLAYER(P  696, T  822): : tv_stream_priv_activate_audio_pipeline(572) > stream_name = ATV:0xaec0d400:M(1), stream = 0xaec0d400, audio_enabled = 0, stream_state = 4
  9190.539 D/TV_PLAYER(P  696, T  822): : tv_player_rm_allocate_audio_resource(698) > call begin                                              // loss "call end"
  9190.539 E/TV_PLAYER(P  696, T  822): : tv_player_rm_allocate_audio_resource(701) > [ATV:0xaec0d400:M(1)]
  9190.540 D/TV_PLAYER(P  696, T  822): : tv_player_rm_allocate_audio_out_resource(402) > call begin
  9190.540 E/TV_PLAYER(P  696, T  822): : tv_player_rm_allocate_audio_out_resource(404) > [ATV:0xaec0d400:M(1)]allocate audio out resource, a_out_id[0]
  9190.540 E/TV_PLAYER(P  696, T  822): : tv_player_rm_get_audio_out_category_id(1926) > [ATV:0xaec0d400:M(1)],Use RM_CATEGORY_AUDIO_MAIN_OUT
  9190.714 I/RSC_CENTER_API(P  696, T  822): resource_center.cpp: rc_get_capable_category_id(45) > player_id(141), app_id(org.tizen.tv-viewer), category(9)
  9191.131 I/RSC_CENTER_API(P  696, T  822): resource_center_private.cpp: _rc_dbus_method_call(138) > Method call 'GetCapableCatId'

改写getline

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <list>
using namespace std;
 
#if 1
#define MAX_LINE_PER_FILE   1000000
#define SIZE_PER_READ       10
#define MAX_READ_TIME       10
#else
#define MAX_LINE_PER_FILE   8
#endif


int get_line (ifstream & in, string & line)
{
    if (!in) {
         cout << "in null" <<endl;
        return 0;
    }

    line = "";

    char change_line[3] = "\r\n"; 
    int i = 0;
    int index = 0;
    char * ptr = NULL;
    char read_buf[SIZE_PER_READ+1] = {};
    bool isFind = false;
    //cout << "-------------" <<endl;

    unsigned int pos = 0;
    pos = in.tellg();

    for(i=0; i<MAX_READ_TIME; i++){
        memset(read_buf, 0, SIZE_PER_READ);
        in.read(read_buf, SIZE_PER_READ);

        if ((ptr = strstr(read_buf, change_line)) != NULL){
            index = ptr - read_buf;          
            in.seekg( 0-(SIZE_PER_READ - index -strlen(change_line))  , ios::cur);
            read_buf[index+strlen(change_line)] = 0;
            line += read_buf;
            isFind = true;
            break;
        } else {
            line += read_buf;
        }
    }

    if(!isFind){
        cout << "no find change line!" << endl;
    }

    int strLen = line.length();
    //cout << "str = " << endl << line;
    //cout << "len = " << strLen << endl;

    return strLen;
} 
 
int main (int argc, char** argv)
{
    unsigned int num_line = 0;
    unsigned int num_read = 0;
    int index = 0;
 
    ofstream out;
    string line;
    string file_out;
    bool isNeedDebug = false;
 
    char str_d[15] = " D/"; 
    char * str_match = NULL; 
 
    if(argc <= 1){
      cout << "need file input" << endl;
      return 0;
    }
 
    if(argc <= 2) {
      cout << "need special string input" << endl;
      return 0;
    }
 
    str_match = argv[2];
    cout << str_match << endl;
 
    if(argc == 4) {
      if((*argv[3] == 'd') || (*argv[3] == 'D')){
        isNeedDebug = true;
        cout << "need d" << endl;
      }
    }
 
    string file_in =  argv[1];
    ifstream in(file_in, ios::in|ios::binary);
 
    if(in) {
      line = "";
      while (get_line(in, line))
      {
        num_read++;
        
        //if (line.find(tvp1) != string::npos)) {
        if(strstr(line.c_str(), str_match) != NULL) {
          if(isNeedDebug == false){
            if(strstr(line.c_str(), str_d) != NULL){
              continue;
            }
          }
 
          if ((num_line % MAX_LINE_PER_FILE) == 0){
            if(file_out.length() > 0){
              out.close();
            }
 
            if(isNeedDebug) {
              file_out = "out_d_" + to_string(index) + ".txt";
            }else{
              file_out = "out_" + to_string(index) + ".txt";
            }
 
            index++;
 
            out.open(file_out, ios::out|ios::binary);
 
            if(!out) {
              cout << "file created faild!" << endl;
            }
          }
 
          out << line << endl;          
          num_line++;
        }        
      }
 
      cout << num_read << endl;
      cout << num_line << endl;
 
      out.close();
    }
    else {
      cout <<"no such file" << endl;
    }
  return 0;
}

提取固定

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <list>
using namespace std;

#define MAX_LINE_PER_FILE   0XFFFFF
#define SIZE_PER_READ       100
#define MAX_READ_TIME       100
#define IF_NEED_ADD_LINE    1
#define IF_ALL_OUTPUT       0

string ver = "20221208 v1.0";
unsigned int file_size = 0;
unsigned int num_read = 0;

void changeNoneAsicCode(char *buf, int len)
{
  for(int i = 0; i<len; i++){
    if((buf[i]<32) || (buf[i]>126)){
      buf[i] = ' ';
    }
  }
}

char * isHasChangeLine(char *buf, int len)
{
  for(int i=0; i<len; i++) {
    if(buf[i] == '\r'){

      if (i < len-1) {

        if (buf[i+1] == '\n') {
          return (char *)&buf[i+1];
        }
        else {
          return (char *)&buf[i];
        }
      } else {
        return NULL;
      }
    }else if(buf[i] == '\n'){
       return (char *)&buf[i];
    }
  }
  return NULL;
}

int get_line (ifstream & in, string & line)
{
    if (!in) {
      return 0;
    }

    line = "";
    unsigned int pos = in.tellg();
    int left_size = file_size-pos;
    if(left_size == 0){
      return 0;
    }

    char change_line[3] = "\n";
    int i = 0;
    int index = 0;
    char * ptr = NULL;
    char read_buf[SIZE_PER_READ+1] = {};
    unsigned int read_size = 0;

    for(i=0; i<MAX_READ_TIME; i++){
      memset(read_buf, 0, SIZE_PER_READ);

      if(left_size>=SIZE_PER_READ){
        read_size = SIZE_PER_READ;
      }else{
        read_size = left_size;
      }

      in.read(read_buf, read_size);

      if ((ptr = isHasChangeLine(read_buf, read_size)) != NULL) {
        index = ptr - read_buf;
        in.seekg( 0-(read_size - index -strlen(change_line))  , ios::cur);
        read_buf[index+strlen(change_line)] = 0;
        line += read_buf;
        break;
      } else {
        line += read_buf;
      }
    }

    num_read++;
    if((num_read & 0XFFFF) == 0) {
      cout << "<";
    }

    int strLen = line.length();
    return strLen;
}

string num2str(int i)
{
    char buf[7];
    snprintf(buf, sizeof(buf), "%6d", i);
    return buf;
}

int main (int argc, char** argv)
{
    unsigned int num_line = 0;
    int index = 0;
    ofstream out;
    string line;
    string file_out;
    bool isNeedDebug = false;
    int line_len = 0;
    int line_change_num = 0;
    char tvp[15] = "/TV_PLAYER(";
    char tvp_d[15] = "D/TV_PLAYER(";

    cout << "version: " << ver << endl;

    if(argc <= 1){
      cout << "need file input" << endl;
      return 0;
    }

    if(argc == 3) {
      if ((strcmp(argv[2], "-d") == 0) || (strcmp(argv[2], "-D") == 0)) {
        isNeedDebug = true;
        cout << "need d" << endl;
      }
    }

    string file_in =  argv[1];
    ifstream in(file_in, ios::in|ios::binary);
    in.seekg(0, ios::end);
    file_size = in.tellg();
    in.seekg(0, ios::beg);

    if(in) {
      while ((line_len = get_line (in, line))>0) {

        #if !IF_ALL_OUTPUT
        if(strstr(line.c_str(), tvp) != NULL) {
          if(isNeedDebug == false) {
            if(strstr(line.c_str(), tvp_d) != NULL){
              continue;
            }
          }
        #endif

          if ((num_line & MAX_LINE_PER_FILE) == 0){
            if(file_out.length() > 0){
              out.close();
            }

            if(isNeedDebug) {
              file_out = "tvp_d_" + to_string(index) + ".txt";
            }else{
              file_out = "tvp_" + to_string(index) + ".txt";
            }

            index++;

            out.open(file_out, ios::out|ios::binary);

            if(!out) {
              cout << "file created faild!" << endl;
            }
          }

          #if IF_NEED_ADD_LINE
          string strOut = "line " + num2str(num_read) + ": ";
          strOut += line;
          out.write(strOut.c_str(), strOut.length());
          #else
          out.write(line.c_str(), strOut.length());
          #endif
          num_line++;
        #if !IF_ALL_OUTPUT
          }
        #endif
      }

      cout << endl;
      cout << num_read << endl;
      cout << num_line << endl;

      out.close();
    } else {
      cout <<"no such file" << endl;
    }
  return 0;
}

提取指定

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <list>
using namespace std;

#define MAX_LINE_PER_FILE   0XFFFFF
#define SIZE_PER_READ       100
#define MAX_READ_TIME       100
#define IF_NEED_ADD_LINE    1
#define IF_ALL_OUTPUT       0

string ver = "20221208 v1.0";
unsigned int file_size = 0;
unsigned int num_read = 0;
unsigned int num_line = 0;
bool isNeedDebug = false;
char str_d[15] = " D/";

void changeNoneAsicCode(char *buf, int len)
{
  for(int i = 0; i<len; i++){
    if((buf[i]<32) || (buf[i]>126)){
      buf[i] = ' ';
    }
  }
}

char * isHasChangeLine(char *buf, int len)
{
  for(int i=0; i<len; i++) {

    if(buf[i] == '\r'){

      if (i < len-1) {

        if (buf[i+1] == '\n') {
          return (char *)&buf[i+1];
        }
        else {
          return (char *)&buf[i];
        }
      } else {
        return NULL;
      }
    }else if(buf[i] == '\n'){
       return (char *)&buf[i];
    }
  }

  return NULL;
}

int get_line (ifstream & in, string & line)
{
    if (!in) {
      return 0;
    }

    line = "";
    unsigned int pos = in.tellg();
    int left_size = file_size-pos;

    if(left_size == 0){
      return 0;
    }

    char change_line[3] = "\n";
    int i = 0;
    int index = 0;
    char * ptr = NULL;
    char read_buf[SIZE_PER_READ+1] = {};
    unsigned int read_size = 0;

    for(i=0; i<MAX_READ_TIME; i++){
      memset(read_buf, 0, SIZE_PER_READ);

      if(left_size>=SIZE_PER_READ){
        read_size = SIZE_PER_READ;
      }else{
        read_size = left_size;
      }

      in.read(read_buf, read_size);

      if ((ptr = isHasChangeLine(read_buf, read_size)) != NULL) {
        index = ptr - read_buf;
        in.seekg( 0-(read_size - index -strlen(change_line))  , ios::cur);
        read_buf[index+strlen(change_line)] = 0;
        line += read_buf;
        break;
      } else {
        line += read_buf;
      }
    }

    num_read++;
    if((num_read & 0XFFFF) == 0) {
      cout << "<";
    }

    int strLen = line.length();
    return strLen;
}

bool strMatch(string strLine, list<string> listMatch)
{
  std::list<string>::iterator itm;
  for (itm=listMatch.begin(); itm!=listMatch.end(); itm++) {
    if (strstr(strLine.c_str(), (*itm).c_str()) != NULL) {
      return true;
    }
  }
  return false;
}

string num2str(int i)
{
  char buf[7];
  snprintf(buf, sizeof(buf), "%6d", i);
  return buf;
}

void getMatch(ofstream & out, string line, list<string> listMatch)
{
  static string file_out;
  static int index = 0;

  #if !IF_ALL_OUTPUT
  if(strMatch(line, listMatch)) {

    if(isNeedDebug == false){
      char *ptr = NULL;
      if((ptr = strstr(line.c_str(), str_d)) != NULL){
        if(*(ptr-4) == '.') {
          return;
        }
      }
    }
  #endif

    if ((num_line & MAX_LINE_PER_FILE) == 0){
      if(file_out.length() > 0) {
        out.close();
      }

      if(isNeedDebug) {
        file_out = "out_d_" + to_string(index) + ".txt";
      }else{
        file_out = "out_" + to_string(index) + ".txt";
      }

      index++;

      out.open(file_out, ios::out|ios::binary);

      if(!out) {
        cout << "file created faild!" << endl;
      }
    }

    #if IF_NEED_ADD_LINE
    string strOut = "line " + num2str(num_read) + ": ";
    strOut += line;
    out.write(strOut.c_str(), strOut.length());
    #else
    out.write(line.c_str(), strOut.length());
    #endif
    num_line++;
  #if !IF_ALL_OUTPUT
  }
  #endif

  return;
}

void getOther(ofstream & out, string line, list<string> listMatch)
{
  static string file_out;
  static int index = 0;

  if(!strMatch(line, listMatch)) {
    if ((num_line & MAX_LINE_PER_FILE) == 0){
      if(file_out.length() > 0) {
        out.close();
      }

      file_out = "exclude_" + to_string(index) + ".txt";
      index++;
      out.open(file_out, ios::out|ios::binary);

      if(!out) {
        cout << "file created faild!" << endl;
      }
    }

    #if IF_NEED_ADD_LINE
    string strOut = "line " + num2str(num_read) + ": ";
    strOut += line;
    out.write(strOut.c_str(), strOut.length());
    #else
    out.write(line.c_str(), strOut.length());
    #endif
    num_line++;
  }
  return;
}

int main (int argc, char** argv)
{
  ofstream out;
  string line;
  bool isExclude = false;
  int line_len = 0;
  string str_match;

  cout << "version: " << ver << endl;

  if(argc < 2){
    cout << "need file input" << endl;
    return 0;
  }

  if(argc < 3) {
    cout << "need special string input" << endl;
    return 0;
  }

  list<string> listMatch;

  if ((strcmp(argv[2], "-d") == 0) || (strcmp(argv[2], "-D") == 0)) {
    isNeedDebug = true;
    cout << "need d" << endl;
  } else if ((strcmp(argv[2], "-v") == 0) || (strcmp(argv[2], "-V") == 0)){
    isExclude = true;
    cout << "exclude" << endl;
  } else {
    str_match = argv[2];
    listMatch.emplace_back(str_match);
  }

  for (int i = 3; i < argc; i++) {
    str_match = argv[i];
    listMatch.emplace_back(str_match);
  }

  string file_in =  argv[1];
  ifstream in(file_in, ios::in|ios::binary);
  in.seekg(0, ios::end);
  file_size = in.tellg();
  in.seekg(0, ios::beg);

  if(in) {
    while ((line_len = get_line (in, line))>0) {
      if (!isExclude) {
        getMatch(out, line, listMatch);
      } else {
        getOther(out, line, listMatch);
      }
    }

    cout << endl;
    cout << num_read << endl;
    cout << num_line << endl;

    out.close();
  } else {
    cout <<"no such file" << endl;
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值