c語言讀寫配置檔案

2021-09-17 03:39:42 字數 2499 閱讀 8547

#include#include#include #define filenm "c:/cfg.ini"

void trim(char *strin, char *strout);//去除字串首位空格

void getvalue(char *keyandvalue, char *key, char *value);

/*define 定義的filenm不能和函式readcfg中的形參filename相同*/

void readcfg(const char *filename, const char * key, const char **value);

void getvalue(char *keyandvalue, char *key, char *value)

p += strlen(key);

trim(p, value);

p = strstr(value, "=");

if(p == null)

p+= strlen("=");

trim(p, value);

p = strstr(value, "=");

if(p != null)

p = value;

trim(p, value);

}void readcfg(const char *filename, const char *skey, const char **value), vtemp[1024] = ;

pf = fopen(filename, "r"); //以唯讀方式開啟

while(!feof(pf))

if(strlen(vtemp) != 0)

else

*value = null;

if(pf != null)

fclose(pf);

}void trim(char *strin, char *strout)

start = temp; //求得頭指標

temp = strin + strlen(strin) - 1; //得到原字串最後乙個字元的指標(不是'\0')

while (*temp == ' ')

end = temp; //求得尾指標

for(strin = start; strin <= end; )

*strout = '\0';

}int writecfg(const char *filename/*in*/, const char *key/*in*/, const char *value/*in*/)會報錯*/

char ftemp[8196] = , fline[1024] = , *fp; //檔案快取陣列

long fsize = 0;

int reg = 0;

int exit = 0;

int i = 0;

pf = fopen(filename, "r+");

if(pf == null)

//獲得檔案大小

fseek(pf, 0, seek_end); // 將檔案指標指向末尾

fsize = ftell(pf);

if(fsize > flen)

fseek(pf, 0, seek_set); //將檔案指標指向開頭

//一行一行的讀,如果存在key則修改value存到快取陣列中

while(!feof(pf))

printf("fline = %s\n", fline);

strcat(ftemp, fline);

}if(exit != 1)

if(pf != null)

fclose(pf);

}end :

if(pf != null)

fclose(pf);

//重新建立乙個以filename命名的檔案

return reg;

}void menu()

int twrite(), value[1024] = ;

printf("請輸入key:");

scanf("%s", key);

printf("請輸入value:");

scanf("%s", value);

printf("\n您輸入的是:%s = %s\n", key, value);

return writecfg(filenm/*in*/,key/*in*/,value/*in*/);

}void tread(), *value;

printf("請輸入key:");

scanf("%s", key);

readcfg(filenm/*in*/,key/*in*/, &value/*out*/);

if(value == null)

printf("\nvalue = %s\n", value);

if(value != null)

}int main()

}system("pause");

return 0;

}

C語言 檔案操作10 配置檔案讀寫

配置檔案讀寫專案 define crt secure no warnings include include include int writefile const char path in char pkey in char pvalue in if pkey null if pvalue nul...

C 讀寫ini配置檔案

配置檔案中經常用到ini檔案,在vc中其函式分別為 寫入.ini檔案 bool writeprivateprofilestring lpctstr lpstring,鍵值,也就是資料 lpctstr lpfilename ini檔案的路徑 讀取.ini檔案 dword getprivateprofi...

讀寫配置檔案

windows作業系統專門為此提供了6個api函式來對配置設定檔案進行讀 寫 getprivateprofileint 從私有初始化檔案獲取整型數值 getprivateprofilestring 從私有初始化檔案獲取字串型值 getprofileint 從win.ini 獲取整數值 getprof...