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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值