昨天写了个一组ini文件读写函数。提供大家使用,已经在XP+VC7.1和FC6+GCC4.1中测试过了,可以跨平台使用。使用标准C写得,支持C++。源程序可以到:我的网络硬盘下载。
/**
*@file inifile.h
*@cpright (C)2007 GEC
*@auther dengyangjun
*@email dyj057@gmail.com
*@version 0.1
*@create 2007-1-14
*@modify 2007-1-14
*@brief declare ini file operation
*@note
*@history
*/
{
#endif int read_profile_string( const char *section, const char *key,char *value, int size, const char *file);
int read_profile_int( const char *section, const char *key,int default_value, const char *file); int write_profile_string( const char *section, const char *key,const char *value, const char *file);
#endif #endif //end of INI_FILE_H_
*@file inifile.h
*@cpright (C)2007 GEC
*@auther dengyangjun
*@email dyj057@gmail.com
*@version 0.1
*@create 2007-1-14
*@modify 2007-1-14
*@brief declare ini file operation
*@note
*@history
*/
#ifndef INI_FILE_H_
#ifdef __cplusplus
{
#endif int read_profile_string( const char *section, const char *key,char *value, int size, const char *file);
int read_profile_int( const char *section, const char *key,int default_value, const char *file); int write_profile_string( const char *section, const char *key,const char *value, const char *file);
#ifdef __cplusplus
};
#endif #endif //end of INI_FILE_H_
/**
*@file inifile.c
*@cpright (C)2007 GEC
*@auther dengyangjun
*@email dyj057@gmail.com
*@version 0.1
*@create 2007-1-14
*@modify 2007-1-14
*@brief implement ini file operation
*@note
*@history
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
{
#endif #define MAX_FILE_SIZE 8096 #define LEFT_BRACE '['
#define RIGHT_BRACE ']' static int load_ini_file(const char *file, char *buf,int *file_size)
{
FILE *in = NULL;
int i=0;
*file_size =0;
assert(buf !=NULL);
if( NULL == in) {
return 0;
}
while((buf[i]=fgetc(in))!=EOF) {
i++;
assert( i < MAX_FILE_SIZE); //file too big
}
buf[i]='
*@file inifile.c
*@cpright (C)2007 GEC
*@auther dengyangjun
*@email dyj057@gmail.com
*@version 0.1
*@create 2007-1-14
*@modify 2007-1-14
*@brief implement ini file operation
*@note
*@history
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include
"inifile.h"#ifdef __cplusplus
{
#endif #define MAX_FILE_SIZE 8096 #define LEFT_BRACE '['
#define RIGHT_BRACE ']' static int load_ini_file(const char *file, char *buf,int *file_size)
{
FILE *in = NULL;
int i=0;
*file_size =0;
assert(file
!=NULL);assert(buf !=NULL);
in = fopen(file,"r");
if( NULL == in) {
return 0;
}
//load initialization file
while((buf[i]=fgetc(in))!=EOF) {
i++;
assert( i < MAX_FILE_SIZE); //file too big
}
buf[i]='
#include <stdio.h>
#include "inifile.h" //main.c
#define BUF_SIZE 256
int main()
{
const char *file ="myconfig.ini";
const char *section = "Db";
const char *key = "XX2";
char value[BUF_SIZE]={0};
printf("test get profile string ");
{
printf("read ini file fail ");
}
else
{
int x = read_profile_int(section,key,0,file);
printf("XX2=%d ", x);
printf("read: [%s] = [%s] ",key,value);
}
if(!write_profile_string(section,"XX2","2writeOK",file))
{
printf("write ini file fail ");
}
else
{
printf("write ini file ok ");
}
return 0;
}
#include "inifile.h" //main.c
#define BUF_SIZE 256
int main()
{
const char *file ="myconfig.ini";
const char *section = "Db";
const char *key = "XX2";
char value[BUF_SIZE]={0};
printf("test get profile string ");
if(!read_profile_string(section, key, value, BUF_SIZE, file))
{
printf("read ini file fail ");
}
else
{
int x = read_profile_int(section,key,0,file);
printf("XX2=%d ", x);
printf("read: [%s] = [%s] ",key,value);
}
if(!write_profile_string(section,"XX2","2writeOK",file))
{
printf("write ini file fail ");
}
else
{
printf("write ini file ok ");
}
return 0;
}