上一篇关于txt解析来复制数组,但是又局限性是,txt写的必须规范,没有空格空行等问题,为了更方便,此处尝试用ini的解析来解决此问题
数组一样
struct s_write_file {
QByteArray * ptr_file;
QString name;
QString city;
unsigned int age;
};
定义的ini文件如下
[NUM]
num = 6
[NUM1]
name = wangxiaohong
city = beijing
age = 20
[NUM2]
name = zhangxiaolei
city = shanghai
age = 16
[NUM3]
name = huxiaohei
city = guangzhou
age = 25
[NUM4]
name = lixiaoming
city = shanxi
age = 32
[NUM5]
name = huangxiaohu
city = zhejiang
age = 29
[NUM6]
name = zhaoxiaoling
city = chongqing
age = 26
其中第一个NUM是表示人数
代码如下
unsigned int all_count = 0;
s_write_file w_file[32];
void Widget::ReadFile()
{
QSettings settings("info.ini",QSettings::IniFormat);
int num = settings.value("NUM/num").toInt(); //先得到数量,也就可以判断w_file[]的参数个数。
qDebug() << num;
for (int i = 0 ;i < num ;i ++) {
QString str;
w_file[all_count].ptr_file = NULL;
str.sprintf("NUM%d/name",i+1);
w_file[all_count].file_name = settings.value(str).toString();
qDebug() << w_file[all_count].name;
str.sprintf("NUM%d/city",i+1);
w_file[all_count].md5_file_name = settings.value(str).toString();
qDebug() << w_file[all_count].city;
str.sprintf("NUM%d/age",i+1);
QString age_string = settings.value(str).toString();
if (age_string.contains("0x",Qt::CaseSensitive)) //判断值里面有没0x来判断是16进制还是10进制
{
age = age_string.toInt(0,16); //此处判断是不是16进制
}
else
{
age = age_string.toInt(0,10); //此处判断是不是10进制
}
w_file[all_count].age = age;
qDebug() << w_file[all_count].age;
all_count++;
}
}