目录
文章目录
前言
一、ini库头文件定义
二、配置文件写接口
2.配置文件读接口
总结
前言
本文主要介绍linux下ini配置文件的读取与写入操作,提供linux c API接口供使用开发。
一、ini库头文件定义
头文件定义如下所示:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef _FILECONF_H_ #define _FILECONF_H_ /* * pcFileName:配置文件全路径名称 * pcKey:配置文件key索引 * pcValue:配置文件key值 * */ int ModifyParam(char *pcFileName, char *pcKey, char *pcValue); int ReadParam(char *pcFileName, char *pcKey, char *pcValue); #endif
二、配置文件写接口
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110int CopyFile(char *pcDstFile, char *pcSrcFile) { FILE *fpr = NULL; FILE *fpw = NULL; int iLen = 0; int iFileLen = 0; int iSize = 0; char acBuf[4096]; if (NULL == pcDstFile || NULL == pcSrcFile) { return -1; } fpr = fopen(pcSrcFile, "rb"); if (fpr == NULL) { perror("open read file:"); return -1; } fpw = fopen(pcDstFile, "wb"); if (fpw == NULL) { printf("[%s]open write file %sn", __FUNCTION__, pcDstFile); perror("open write file error:"); return -1; } while (1) { iLen = fread(acBuf, 1, sizeof(acBuf), fpr); if (iLen <= 0) { printf("read file overn"); break; } iSize = fwrite(acBuf, 1, iLen, fpw); if (iSize != iLen) { perror("wirte file error:"); fclose(fpr); fclose(fpw); return -1; } iFileLen += iLen; } fflush(fpw); fsync(fileno(fpw)); fclose(fpr); fclose(fpw); return iFileLen; } int ModifyParam(char *pcFileName, char *pcKey, char *pcValue) { FILE *pOldFile = NULL; FILE *pNewFile = NULL; char acTmpName[128]; char acBuf[256]; int iFlag = 0; pOldFile = fopen(pcFileName, "rb"); if (NULL == pOldFile) { printf("[%s] open file %s errorn", __FUNCTION__, pcFileName); perror(":"); return -1; } snprintf(acTmpName, sizeof(acTmpName), "%s~", pcFileName); pNewFile = fopen(acTmpName, "wb"); if (NULL == pNewFile) { printf("[%s] open file %s errorn", __FUNCTION__, acTmpName); perror(":"); fclose(pOldFile); return -1; } while (fgets(acBuf, sizeof(acBuf), pOldFile)) { if (memcmp(acBuf, pcKey, strlen(pcKey)) == 0) { snprintf(acBuf, sizeof(acBuf), "%s=%sn", pcKey, pcValue); fputs(acBuf, pNewFile); iFlag++; } else { fputs(acBuf, pNewFile); } } // 如果没有此项,则添加一项 if (0 == iFlag) { snprintf(acBuf, sizeof(acBuf), "%s=%sn", pcKey, pcValue); fputs(acBuf, pNewFile); } fflush(pNewFile); fsync(fileno(pNewFile)); fclose(pOldFile); fclose(pNewFile); //rename(acTmpName, pcFileName); CopyFile(pcFileName, acTmpName); return 0; }
2.配置文件读接口
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41int ReadParam(char *pcFileName, char *pcKey, char *pcValue) { FILE *pFile = NULL; char acBuf[128]; char *pcPos = NULL; int iLen = 0; int iRet = -1; pFile = fopen(pcFileName, "rb"); if (NULL == pFile) { printf("[%s] open file %s errorn", __FUNCTION__, pcFileName); perror(":"); return -1; } while (fgets(acBuf, sizeof(acBuf), pFile)) { if (memcmp(acBuf, pcKey, strlen(pcKey)) == 0) { pcPos = acBuf; strsep(&pcPos, "="); if (NULL == pcPos) { printf("line:%s, param error!!!n", acBuf); break; } iLen = strlen(pcPos); if (iLen > 0) pcPos[iLen-1] = ''; strcpy(pcValue, pcPos); iRet = 0; break; } } fclose(pFile); return iRet; }
总结
本文主要对ini文件读写封装成api接口函数库,供开发者使用。
ini配置开发api库下载链接:
linuxc,ini配置文件读写模块-C文档类资源-CSDN下载
最后
以上就是搞怪树叶最近收集整理的关于linux c-ini配置文件读写模块前言一、ini库头文件定义二、配置文件写接口总结的全部内容,更多相关linux内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复