c语言配置文件,C语言读写配置文件的方法
c语⾔配置⽂件,C语⾔读写配置⽂件的⽅法本⽂实例讲述了C语⾔读写配置⽂件的⽅法。分享给⼤家供⼤家参考。具体如下:
CException.h如下:
/************************************************************************/
/* make0000@msn */
/************************************************************************/
/************************************************************************/
#include "stdio.h"
#include "conio.h"
#include "signal.h"
#include "setjmp.h"
#include "assert.h"
#ifdef __cplusplus
#include "iostream"
8月12日是什么日子#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;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。