运用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;
}
//写ini文件
void writeIniFile(){
char name[16] = "lxj";
char buf[4096 * 10] = { 0 };
int offset = 0;
offset += sprintf(buf + offset, "[label]\n");
offset += sprintf(buf + offset, "name=%s\n", name);
··········
char filename[128] = { 0 };
sprintf(filename, "%s", "/home/etc/mine.ini");
writeFile(buf, filename);
}
//主函数
int main(){
writeIniFile();
return 0;
}
二、读写json格式的配置文件
1.读json文件
//读json函数
cJSON* readJsonFile(char* name) {
FILE* fd = fopen(name, "rb+");
if (fd == NULL) {
return NULL;
}
fseek(fd, 0, SEEK_END);
int len = ftell(fd);
char* content = (char*)malloc(len + 1);
fseek(fd, 0, SEEK_SET);
fread(content, 1, len, fd);
fclose(fd);
cJSON* body_json = cJSON_Parse(content);
free(content);
return body_json;
}
//主函数
int main(){
cJSON* file_json = readJsonFile("/home/etc/mine.json");
if (NULL == file_json) {
//读取失败
}
else {
//数据处理
}
cJSON_Delete(file_json);
}
2.写json文件
//写json函数
void writeJsonFile(cJSON* root, char* filename) {
FILE* fd = fopen(filename, "wb");
char* send_str = cJSON_Print(root);
size_t str_size = strlen(send_str);
fwrite(send_str, str_size, 1, fd);
fclose(fd);
free(send_str);
}
//主函数
int main(){
writeJsonFile();
return 0;
}
三、读写csv格式的配置文件
1.读csv文件
//拆分函数
int split_new(char *str, char *delimiter,char* strCopy,char* retbuf) {
int len = strlen(str);
//复制str字符串,并将strCopy中的每个分隔符赋值为'\0'
strcpy(strCopy, str);
for (int i = 0; strCopy[i] != '\0'; i++) {
for (int j = 0; delimiter[j] != '\0'; j++) {
if (strCopy[i] == delimiter[j]) {
strCopy[i] = '\0';
break;
}
}
}
//为字符串数组分配空间,额外分配多一个字符串指针并赋值为NULL来作为字符串结束标志
//遍历到strCopy最后的'\0'才结束
char** res = (char**)retbuf;
len++;
//每一个分隔符和原字符串的'\0'标志依次作为数组中的字符串的结束标志
int j= 0;
int num = 0;
for (int i = 0; i < len; i++) {
res[j++] = strCopy + i;
num++;
while (strCopy[i] != '\0') {
i++;
}
}
res[resI] = NULL; //字符串数组中结束标志
return num;
}
//数据处理
void dealData(){
char temp_buf[4096] = { 0 };
char ret[4096] = { 0 };
//去掉最后一列中没用的回车或者换行
buf[strlen(buf) - 1] = '\0';
//拆分逗号
int n = split_new(buf, (char *)",", temp_buf, ret);
//逐个提取并存储
char* name = split_strs[0];
char* age = split_strs[1];
······
}
//读csv文件函数
void readCsvFile(){
char file_name[128] = { 0 };
sprintf(file_name, "/home/etc/config.csv");
FILE* fd = fopen(file_name, "rb+");
if (fd == NULL) {
printf("fopen %s failed \n", file_name);
return 0;
}
while (true) {
fgets(buf, 1024, fd);
if (feof(fd)) {
break;
}
if (buf[0] == '\0') {
break;
}
//第一行为标题不读取
if (line != 0) {
//数据处理
dealData();
}
line++;
}
fclose(fd);
}
//主函数
int main(){
readCsvFile();
return 0;
}
2.写csv文件
void writeCsvFile(){
char file_name[128] = { 0 };
sprintf(file_name, "/home/etc/config.csv");
FILE* fd = fopen(file_name, "wb+");
if (fd == NULL) {
printf("fopen %s failed \n", file_name);
}
//数据处理
fclose(fd);
}
//主函数
int main(){
writeCsvFile();
return 0;
}