解析txt文件,赋值结构体数组

来源:互联网 发布:软件字体乱码 编辑:程序博客网 时间:2024/06/15 21:56

qt可以利用自己的库来解析txt文件,赋值到数组中供程序使用,代码如下

首先定义一个数组,比如

struct s_write_file {    QByteArray * ptr_file;    QString  name;    QString  city;    unsigned int age;};

在程序中

unsigned int all_count = 0;s_write_file w_file[32];

void Widget::ReadFile(){    QFile fileRead("/mnt/info.txt");        fileRead.open(QFile::ReadOnly | QFile::Text);        QTextStream in(&fileRead);        QString strContent="";    int file_num= 0;          while(!in.atEnd())    {        strContent += in.readLine();        file_num++;    }     fileRead.flush();     fileRead.close();     //    s_write_file w_file[W_FILE_NUM];  //此处可以判断txt的行数来判断数组的参数个数,不需要直接开w_file[32]        QStringList strlist = strContent.split(";"); //利用;来间隔     for(int i=0;i<strlist.count();i++)     {         QStringList strstrlist=strlist.at(i).split("#");//利用#来间隔        for(int j=0;j<strstrlist.count();)         {             QString file = strstrlist.at(j++).toLocal8Bit().data();             QString md5 = strstrlist.at(j++).toLocal8Bit().data();             QString age_String =strstrlist.at(j++).toLocal8Bit().data();             int age = age_String.toInt(0,10); //10进制计数             w_file[all_count].ptr_file = NULL;             w_file[all_count].name = file;             //qDebug() << w_file[all_count].name;             w_file[all_count].city = city;            //qDebug() << w_file[all_count].city;            w_file[all_count].age= age;             //qDebug() << w_file[all_count].age; all_count++;         }     }}



具体的txt文件如下
wangxiaohong#beijing#20;zhangxiaolei#shanghai#16;huxiaohei#guangzhou#25;lixiaoming#shanxi#32;huangxiaohu#zhejiang#29;zhaoxiaoling#chongqing#26


0 0