配置文件的格式以下:數組
key1 = value1緩存
key2 = value2函數
.測試
.spa
.指針
名值對以一個=連接,一條記錄以換行符分割code
頭文件:blog
#include<stdio.h> #include<stdlib.h> #include <string.h>
函數原型:字符串
void trim(char *strIn, char *strOut);//去除字符串首位空格 void getValue(char * keyAndValue, char * key, char * value); //根據key獲得value int writeCFG(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/); //寫入配置文件 void readCFG(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/); //讀取配置文件
函數實現:get
1 void trim(char *strIn, char *strOut){ 2 3 char *start, *end, *temp;//定義去除空格後字符串的頭尾指針和遍歷指針 4 5 temp = strIn; 6 7 while (*temp == ' '){ 8 ++temp; 9 } 10 11 start = temp; //求得頭指針 12 13 temp = strIn + strlen(strIn) - 1; //獲得原字符串最後一個字符的指針(不是'\0') 14 15 while (*temp == ' '){ 16 --temp; 17 } 18 19 end = temp; //求得尾指針 20 21 22 for(strIn = start; strIn <= end; ){ 23 *strOut++ = *strIn++; 24 } 25 26 *strOut = '\0'; 27 } 28 29 void getValue(char * keyAndValue, char * key, char * value){ 30 31 char *p = keyAndValue; 32 33 p = strstr(keyAndValue, key); 34 if(p == NULL){ 35 //printf("沒有key\n"); 36 return ; 37 } 38 39 p += strlen(key); 40 trim(p, value); 41 42 p = strstr(value, "="); 43 if(p == NULL){ 44 printf("沒有=\n"); 45 return; 46 } 47 p+= strlen("="); 48 trim(p, value); 49 50 p = strstr(value, "="); 51 if(p != NULL){ 52 printf("多餘的=\n"); 53 return; 54 } 55 p = value; 56 trim(p, value); 57 58 } 59 int writeCFG(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/){ 60 61 FILE *pf = NULL; 62 char ftemp[flen] = {0}, fline[1024] = {0}, *fp; //文件緩存數組 63 long fsize = 0; 64 int reg = 0; 65 int exit = 0; 66 int i = 0; 67 68 pf = fopen(filename, "r+"); 69 if(pf == NULL){ 70 pf = fopen(filename, "w+"); 71 } 72 //得到文件大小 73 fseek(pf, 0, SEEK_END); // 將文件指針指向末尾 74 fsize = ftell(pf); 75 if(fsize > flen){ 76 printf("文件不能超過8k\n"); 77 reg = -1; 78 goto end; 79 } 80 fseek(pf, 0, SEEK_SET); //將文件指針指向開頭 81 82 //一行一行的讀,若是存在key則修改value存到緩存數組中 83 while(!feof(pf)){ 84 fgets(fline, 1024, pf); 85 if(strstr(fline, key) != NULL && exit == 1) 86 strcpy(fline, ""); 87 if(strstr(fline, key) != NULL && exit == 0){ //判斷key是否存在 88 exit = 1; 89 sprintf(fline,"%s = %s\n", key, value); 90 } 91 92 printf("fline = %s\n", fline); 93 strcat(ftemp, fline); 94 95 } 96 if(exit != 1){//若是不存在則把key value寫入到最後一行 97 sprintf(fline,"%s = %s\n", key, value); 98 strcat(ftemp, fline); 99 } 100 if(pf != NULL){ 101 fclose(pf); 102 pf = fopen(filename, "w+"); 103 fp = (char *)malloc(sizeof(char) * strlen(ftemp) + 1); 104 strcpy(fp, ftemp); 105 fp[strlen(fp) - 1] = EOF; 106 fputs(fp, pf); 107 if(fp != NULL){ 108 free(fp); 109 fp = NULL; 110 } 111 fclose(pf); 112 } 113 end : 114 if(pf != NULL) 115 fclose(pf); 116 //從新建立一個以filename命名的文件 117 return reg; 118 } 119 120 void readCFG(const char *filename/*in*/, const char *key/*in*/, const char **value/*out*/){ 121 122 FILE *pf = NULL; 123 char line[1024] = {0}, vtemp[1024] = {0}; 124 125 pf = fopen(filename, "r"); //以只讀方式打開 126 127 while(!feof(pf)){ 128 fgets(line, 1024, pf); 129 getValue(line, key, vtemp); 130 if(strlen(vtemp) != 0) 131 break; 132 } 133 if(strlen(vtemp) != 0){ 134 *value = (char *)malloc(sizeof(char) * strlen(vtemp) + 1); 135 strcpy(*value, vtemp); 136 } 137 else 138 *value = NULL; 139 if(pf != NULL) 140 fclose(pf); 141 }
測試:
1 #define filename "c:/cfg.ini" 2 void menu(){ 3 printf("===========================\n"); 4 printf("1 寫入配置文件\n"); 5 printf("2 讀取配置文件\n"); 6 printf("0 退出程序"); 7 printf("===========================\n"); 8 } 9 10 int tWrite(){ 11 12 char key[1024] = {0}, value[1024] = {0}; 13 14 printf("請輸入key:"); 15 scanf("%s", key); 16 printf("請輸入value:"); 17 scanf("%s", value); 18 printf("\n您輸入的是:%s = %s\n", key, value); 19 20 return writeCFG(filename/*in*/,key/*in*/,value/*in*/); 21 } 22 23 void tRead(){ 24 char key[1024] = {0}, *value; 25 26 printf("請輸入key:"); 27 scanf("%s", key); 28 29 readCFG(filename/*in*/,key/*in*/, &value/*out*/); 30 if(value == NULL){ 31 printf("沒有key\n"); 32 return ; 33 } 34 printf("\nvalue = %s\n", value); 35 36 if(value != NULL){ 37 free(value); 38 value = NULL; 39 } 40 41 } 42 int main(){ 43 44 int choose; 45 46 while(1){ 47 choose = 0; 48 menu(); 49 printf("請輸入選擇:"); 50 scanf("%d", &choose); 51 switch(choose){ 52 case 1: 53 if(tWrite() == -1) 54 return -1; 55 break; 56 case 2: 57 tRead(); 58 break; 59 case 0: 60 return 0; 61 default: 62 return 0; 63 } 64 } 65 system("pause"); 66 return 0; 67 }
運行結果: