`
dmp63dmp
  • 浏览: 16978 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

C语言读写配置文件

 
阅读更多

C语言读写配置文件
2011年03月04日
  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  #include  #include  #include  #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(); //写入节. 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  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]="武汉软件工程职业学院计算机应用系孟德军"; int i=0; for(i=0;i typedef (* write)(char* filename,char* key,char* value); typedef (* writes)(char* filename,char* value); typedef (* read)(char* filename,char* key,char* value); HINSTANCE hInstance; write write_file; writes write_section; read read_file; char buffer[128]; char buffer1[128]; void main() { hInstance=LoadLibrary("File.dll"); if(hInstance!=NULL) { write_file=(write)GetProcAddress(hInstance,"write_ file"); write_section=(writes)GetProcAddress(hInstance,"wr ite_section"); read_file=(read)GetProcAddress(hInstance,"read_fil e"); write_section("config.ini","mysql"); write_file("config.ini","username","root"); write_file("config.ini","password","admin"); write_file("config.ini","driverClassName","com.mys ql.jdbc.Driver"); write_file("config.ini","url","jdbc:mysql://localh ost:3306/user"); read_file("config.ini","username",buffer); printf("username=%s",buffer); read_file("config.ini","password",buffer1); printf("password=%s",buffer1); } else { printf("can't load dynamic file File.dll"); } FreeLibrary(hInstance); } 
  
  
  
  
  
  
  
  
  
  
  
分享到:
评论

相关推荐

    纯C语言读写ini配置文件

    使用纯C语言写的读写ini格式配置文件示例,VC6编译,代码和注释详细。封装合理,可快速集成到其它项目。

    C语言读写配置文件的方法

    主要介绍了C语言读写配置文件的方法,包括C语言读写ini配置文件所涉及的文件读写技巧,以及完整的源文件及头文件实现方法,需要的朋友可以参考下

    C语言实现读写配置文件

    用纯C语言实现配置文件的读写函数,可代替WritePrivateProfileString和GetPrivateProfileString函数. 

    用c语言实现配置文件读写

    用c语言实现配置文件读写,完整的函数,适当的注释,值得的参考!

    linux配置文件读写好用的类

    有了它,就不用为在linux下读写配置文件烦恼了。 经实际项目使用测试,绝对好用

    C语言实现的读写配置文件函数,与Windows API函数功能一样

    利用纯C语言实现的配置文件.ini读写函数,可跨平台使用。功能与Windows API函数 WritePrivateProfileString 和GetPrivateProfileString 一样

    linux 配置文件读写C

    这是个不错的c代码,可以用在linux环境下c语言读写配置文件,转给大家下载。

    纯c读写ini配置文件

    纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种...

    用C语言读取.ini文件

    在Windows下可以用GetPrivateProfileString或GetPrivateProfileInt方便读取.ini配置文件内容,自己尝试写了一下这个函数。还有一些bug,后面慢慢改。

    C语言配置文件读写.zip

    C语言配置文件读写.zip

    标准C/C++读写配置文件类,读写ini文件,加入工程就可以用

    标准C/C++读写配置文件类,读写ini文件,加入工程就可以用. 非常完美的封装类,利用SET容器储存,速度非常快。

    INI文件读写C语言代码(可跨系统)

    使用标准C库写的INI读写文件,支持INI文件注释。 当前版本在WINDOWS下用codeblock编译测试通过,稍作修改(知道linux windows下换行符的差别即可)即可移植到linux系统中。欢迎交流!

    WINCE下读写INI配置文件

    WINCE下读写INI配置文件,支持ANSI及UNICODE编码。C语言实现,高效简洁,调用接口及实现效果与系统API近乎一致。

    c语言实现ini文件读写操作

    C语言实现的INI文件读写操作是软件开发中常用的一种配置管理技术。INI文件以其简单的结构和易于编辑的特性,成为了存储程序设置和配置信息的首选格式。通过使用C语言编写的读写操作函数,开发者可以轻松地在程序中...

    c ini配置文件的读写

    ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言读写ini 使用c语言...

    文本配置文件读写

    这个代码用于读写配置文件,分为Section、Key、Value三部分,用C语言编写,适合各种应用场合,非常实用,代码精简高效,单片机也能使用,linux、windos就更不在话下了。

    读写配置文件RdWrConfig

    在LINUX平台下C语言实现的配置文件的读写,非常方便,可用于嵌入式开发。 写配置函数实现:int WritePrivateProfileString(char *section,char *key,char *val,char *filePath) 读配置函数实现:int ...

    VC读写ini配置文件代码例程

    C语言写的读取INI信息例程,对自己的ini文件操作的Api例程.GetiniString_App

    linux c,ini配置文件读写模块

    linux c,ini配置文件读写模块,主要用于对ini配置文件进行对鞋操作

Global site tag (gtag.js) - Google Analytics