使用 C 进行 INI 文件操作
使用 C 读取和写入 INI 文件

引言
本项目使用 C 和链表来实现和管理 INI 文件。本项目是链表实现的一个示例。
示例用法
ini_start("sample.ini");
//save_as("test.ini");
buffer = get_value("Main","PLC");
printf("value : %s\n",buffer);
printf("last error is %s\n",get_last_error());
//print_content();
ini_end();
如何实现
本项目使用链表构建一个示例树,用于收集 INI 文件数据。本项目中的 INI 文件必须使用“=”分隔。这是一个管理 INI 文件的示例函数。
/*****************************************************************/
/* main ini manage function */
/************************************************************************/
/**
* function: ini_start
* parameter:
* @filename
* return: true if success
* purpose: for start ini manipulate file
*/
bool ini_start(const char* filename);
/**
* function: load
* parameter:
* @filename
* return: true if success
* purpose: for load ini file to content
*/
bool load(const char *filename);
/**
* function: _save
* parameter:
* @filename
* return: true if success
* purpose: save content to ini file(save as)
*/
bool save(); // save to load filebool
bool save_as(const char *filename);
/**
* function: get_value
* parameter:
* @
* return: value
* purpose: ?
*/
char *get_value (const char *sec,const char *key);
char *_get_value(const char *sec,const char *key, // return data and comment
char *comment);
/**
* function: set_value
* parameter:
* @
* return: true if success
* purpose:
*/
bool set_value (const char *sec,const char *key, // will auto replace
const char *value);
bool _set_value (const char *sec,const char *key, // select replace or not replace
const char *value,const char *comment,REPLACE_FLAG flag);
int remove_sel (const char *sec,char *key);
int remove_all (const char * sec); // remove all record in section
// add/remove section
void add_section(const char *sec,const char *comment); // add section
int remove_section(char *sec); // remove section (remove all record
// in section if not empty)
void clear(); // clear all content
// size of section
int content_size();
int section_size(char *sec);
// for console display use stdio.h stdout
void print_content(); // print all content
void print_section(); // print all only section
void print_record(char *sec,char *key); // print selection record
void print_allrecord(char *sec); // print all record in section
/**
* function: ini_end
* parameter:
* none
* return: void
* purpose: for end ini manipulate file
*/
void ini_end();
void _ini_end(REPLACE_FLAG flag);
/**
* function: get_last_error
* parameter:
* none
* return: type of error
* purpose: for get error
*/
char *get_last_error();
历史
- 2006 年 8 月 22 日:初始发布