C語言讀寫配置文件的方法
本文實例講述了C語言讀寫配置文件的方法。分享給大家供大家參考。具體如下:
CException.h如下:
/************************************************************************/ /* make0000@msn.com */ /************************************************************************/ /************************************************************************/ #include "stdio.h" #include "conio.h" #include "signal.h" #include "setjmp.h" #include "assert.h" #ifdef __cplusplus #include "iostream" #include "exception" extern "C"{ #define dllexport __declspec(dllexport) jmp_buf Jmp_Buf; int E; #define Exception 0x00000 #define e Exception #define try if(!(E=setjmp(Jmp_Buf))) #define last_error() E #define catch(val) else #define throw(val) longjmp(Jmp_Buf,val) #define check(expersion) assert(expersion) #define GetError() errno dllexport void sig_usr(int); dllexport char* getTime(); } #else #define dllexport __declspec(dllexport) jmp_buf Jmp_Buf; int E; #define Exception 0x00000 #define e Exception #define try if(!(E=setjmp(Jmp_Buf))) #define last_error() E #define catch(val) else #define throw(val) longjmp(Jmp_Buf,val) #define check(expersion) assert(expersion) #define GetError() errno dllexport void sig_usr(int); dllexport char* getTime(); #endif
File.h如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #define SIZE 128 #include "CException.h" #define export __declspec(dllexport) //讀取配置文件. int read_file(char* filename,char* key,char* value); //寫配置文件. int write_file(char* filename,char* key,char* value); //釋放文件. int release(); //寫入節(jié). int write_section(char* filename,char* section); int read_section(char* filename); int getAuthor(char* value); void getVersion(char* value);
File.c如下:
#include "File.h" #include <string.h> int read_file(char* filename,char* key,char* value) { int flag=0; char buffer[SIZE]; FILE *file=fopen(filename,"r"); try { if(file==NULL) { flag=1; throw(flag); } else { while(fgets(buffer,SIZE,file)!=NULL) { int i=0,j=0,len=strlen(key); while(buffer[i]!='\0') { if(buffer[i]=='$'&&buffer[i+len+1]=='=') { j=i+len+2; while(buffer[j]!='\0'&&buffer[j]!=';') { int h=0; if(buffer[i+1]==key[i]) { //printf("%c",buffer[j]); value[j-i-len-2]=buffer[j]; } j++; } break; } else if(buffer[i]=='/'&&buffer[i+1]=='/'||buffer[i]==';') { break; //comment } i++; } } } } catch(Exception) { flag=2; fclose(file); printf("can't open file %s",filename); exit(1); } fflush(file); fclose(file); return flag; } int write_file(char* filename,char* key,char* value) { int flag=0; FILE* file; file=fopen(filename,"a"); try { if(file==NULL) { flag=1; throw(flag); } fprintf(file,"$%s=%s\n",key,value); } catch(Exception) { printf("Can't write file %s",filename); exit(1); } fflush(file); fclose(file); return flag; } int write_section(char* filename,char* section) { int flag=0; FILE* file=NULL; try { file=fopen(filename,"a"); if(file!=NULL) { fprintf(file,"[%s]\n",section); } else { int flag=1; throw(flag); } } catch(Exception) { printf("can't open file %s",filename); exit(0); } fflush(file); fclose(file); return flag; } int release() { int flag=1; return flag; } int read_section(char* filename) { return 0; } int getAuthor(char* value) { char author[128]="武漢軟件工程職業(yè)學(xué)院計算機應(yīng)用系"; int i=0; for(i=0;i<strlen(author);i++) { value[i]=author[i]; } return 0; } void getVersion(char* value) { char version[128]="2009//05//01"; int i=0; for(i=0;i<strlen(version);i++) { value[i]=version[i]; } } /************************************************************************** void main() { char* str=NULL; char author[120]; char buffer[128]; char buffer1[128]; char buffer2[128]; read_file("F:\\exercise\\C++!C\\sys.ini","password",buffer); read_file("F:\\exercise\\C++!C\\sys.ini","username",buffer1); read_file("F:\\exercise\\C++!C\\sys.ini","driver",buffer2); printf("password=%s\n",buffer); printf("\n"); printf("username=%s\n",buffer1); printf("\n"); printf("driver=%s\n",buffer2); getAuthor(author); printf("\n"); printf("author=%s",author); release(); }
希望本文所述對大家的C語言程序設(shè)計有所幫助。
上一篇:C++遍歷文件夾下文件的方法
欄 目:C語言
下一篇:C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本文標(biāo)題:C語言讀寫配置文件的方法
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2958.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子