运用C语言读写配置文件中.ini或.json或.csv合适的文件方法
一、读写ini格式的配置文件
1.读ini文件
//读ini文件
void readIniFile(){
//1.创建一个字典指针
dictionary* dict = NULL;
//2.读取配置文件(路径)到字典
char section[128] = { 0 };
sprintf(section, "%s", "/home/etc/mine.ini");
dict = iniparser_load(section);
//3.获取string类型信息
char name[64] = { 0 };
sprintf(section, "%s:%s", "label", "name");
strcpy(name, iniparser_getstring(dict, section, "error"));
//4.获取int类型信息
sprintf(section, "%s:%s", "label", "join");
int join = iniparser_getint(dict, section, 0);
}
//主函数
int main(){
readIniFile();
return 0;
}
2.写ini文件
int writeFile(char* buf, char* filename) {
FILE* fd = fopen(filename, "wb+");
fprintf(fd, "%s", buf);
fflush(fd);
fclose(fd);
return 0;