#include <QSettings >
//创建配置文件
QSettings iniFile("./test.ini", QSettings::IniFormat);
//写入数据
iniFile.setValue("/setting/value1", 1);
iniFile.setValue("/setting/value2", 2);
iniFile.setValue("/log/value3", 3);
//读取数据
QString value1=iniFile.value("/setting/value1").toString();
qDebug() << value1;
QString value2=iniFile.value("/setting/value2").toString();
qDebug() << value2;
效果如下图:
//#include <stdio.h>
FILE* fp;
fp = fopen("./test.ini", "r+");
if(fp == NULL){
printf("fopen error");
}
while(!feof(fp)){
char rr[50] = {'\0'};
if(fgets(rr, 50, fp) != NULL){
printf("rr = %s\n", rr);
}
else{
printf("rr is NULL");
}
}
/* 打印结果
* rr = [setting]
* rr = value1=1
* rr = value2=2
* rr =
* rr = [log]
* rr = value3=3
* rr is NULL
*/
//#include <stdio.h> #include <string>
FILE* fp;
fp = fopen("./test.ini", "r+");
if(fp == NULL){
printf("fopen error");
}
while(!feof(fp)){
char rr[50] = {'\0'};
if(fgets(rr, 50, fp) != NULL){
// printf("rr = %s\n", rr);
std::string str(rr);
int ipos = str.find_first_of("=");
printf("ipos = %d\n", ipos);
if(ipos == -1) continue;
char r1[10], r2[10];
memcpy(r1, rr, ipos);
memcpy(r2, &rr[ipos+1], strlen(rr) - ipos);
printf("r1 = %s\n", r1);
printf("r2 = %s\n\n\n", r2);
}
else{
printf("rr is NULL");
}
}
/* 打印结果
* ipos = -1
* ipos = 6
* r1 = value1
* r2 = 1
*
* ipos = 6
* r1 = value2
* r2 = 2
* ipos = -1
* ipos = -1
* ipos = 6
* r1 = value3
* r2 = 3
* rr is NULL
*/