从大的文本文件读入Qt中的结构数组? [英] Reading from a large text file into a structure array in Qt?

查看:364
本文介绍了从大的文本文件读入Qt中的结构数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读一个文本文件导入structures.I的阵列已经写了一个程序,但它正在因为有该文件中约13紫胶结构太多时间。
请建议我最好的和最快的方式++做这在C

这是我的code:

 的std :: ifstream的input_counter(D:\\\\ cont.txt);/ ******************* ********* /
INT计数器= 0;
而(函数getline(input_counter,线))
{
    ReadCont(线和放大器;合同[计​​数器]); //函数读取数据结构
    反++;
    line.clear();
}
input_counter.close();


解决方案

让你的解析尽可能简单:在这里你知道字段'格式运用所学知识,比如

  ReadCont(| PE | 1 | 0 | 0 | 0 | 0 | 1 | 1 || 2 | 0 || 2 | 0 || 3 | 0 | ... 。,...)

应适用快速CHAR到整数的转换,像

  ReadCont(为const char *线,合同和C){
   如果(行[1] =='P'和;&放大器;线[2] =='E'和;&放大器;线[3] =='|'){
     线+ = 4;
     对于(INT场= 0;场及LT; K_FIELDS_PE ++场){
       c.int_field [现场] = *行++ - '0';
       断言(*线=='|');
       ++线;
     }
   }

好,谨防细节,但你上心......

I have to read a text file into a array of structures.I have already written a program but it is taking too much time as there are about 13 lac structures in the file. Please suggest me the best possible and fastest way to do this in C++.

here is my code:

std::ifstream input_counter("D:\\cont.txt");

/**********************************************************/
int counter = 0;
while( getline(input_counter,line) )
{
    ReadCont( line,&contract[counter]); // function to read data to structure
    counter++;
    line.clear();
}
input_counter.close();

解决方案

keep your 'parsing' as simple as possible: where you know the field' format apply the knowledge, for instance

ReadCont("|PE|1|0|0|0|0|1|1||2|0||2|0||3|0|....", ...)

should apply fast char to integer conversion, something like

ReadCont(const char *line, Contract &c) {
   if (line[1] == 'P' && line[2] == 'E' && line[3] == '|') {
     line += 4;
     for (int field = 0; field < K_FIELDS_PE; ++field) {
       c.int_field[field] = *line++ - '0';
       assert(*line == '|');
       ++line;
     }
   }

well, beware to details, but you got the idea...

这篇关于从大的文本文件读入Qt中的结构数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆