ownthink / KnowledgeGraphData

史上最大规模1.4亿中文知识图谱开源下载

Home Page:https://www.ownthink.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

c++ 读取 偏移 查询显示

hggq opened this issue · comments

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
  if ( argc < 3 ){
    std::cout<<argv[0]<<"  file.csv offset(int) show_num_line(int); "<<std::endl;
    return 1;
  }
  std::string data(argv[1]);
  int offset=0,showline=10;
  
  if(argc>2)
  offset=std::atoi(argv[2]);

  if(argc>3)
    showline=std::atoi(argv[3]);

   std::ifstream in(data.c_str());
   if (!in.is_open()){
      std::cout<<" open csv file error!"<<std::endl;
      return 1;
   }
    std::string line;
    int i=0,j=showline;
    while (getline(in,line))
    {
      if(i>offset){
        if(j>0){
          std::cout<<line<<std::endl;
          j--;
        }else{
          break;
        }
      }
      i++;
    }	
 return 0;
}

可以自己加上查询

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
  if ( argc < 3 ){
    std::cout<<argv[0]<<"  file.csv offset(int) show_num_line(int); "<<std::endl;
    return 1;
  }
  std::string data(argv[1]),search;
  int offset=0,showline=10,isfind=0;
  
  if(argc>2)
  offset=std::atoi(argv[2]);

  if(argc>3)
    showline=std::atoi(argv[3]);

  if(argc>4){
      search=(argv[4]);
      isfind=1;
  }
  

   std::ifstream in(data.c_str());
   if (!in.is_open()){
      std::cout<<" open csv file error!"<<std::endl;
      return 1;
   }

    std::string line;
    int i=0,j=showline;
    std::string::size_type position;
    while (getline(in,line))
    {
      if(isfind==1){
            //在一行中查找
            position = line.find(search);
            int x=0;
            if (position != line.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
            {
              std::cout<<i<<" "<<line<<std::endl;
              i++;
              /*
              for(int n=0;n<9;n++){
                if(getline(in,line)){
                  std::cout<<i<<" "<<line<<std::endl;
                }
              }
             */
              while(x==0){
                  std::cin>>x;
                  if(x>0&&x<10){
                    for(int n=0;n<x;n++){
                      if(getline(in,line)){
                        std::cout<<i<<" "<<line<<std::endl;
                        i++;
                      }
                    }
                   }
                   if(x==8||x==9){
                     break;
                   }else{
                     x=0;
                   }
                   
              }
              
            }
            if(x==9){
               break;
            }
            position = line.npos;
      }else{
        if(i>offset){
        if(j>0){
          std::cout<<i<<" "<<line<<std::endl;
          j--;
        }else{
          break;
        }
       }
      }
      
      i++;
    }	
 return 0;
}

./a.out ownthink_v2.csv 100 10 兴坪渔村
输入 8 继续查
输入 9 退出
输入其他数字 显示查到下面几行