Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > C/C++跨平台ini文件读写API
【标  题】:C/C++跨平台ini文件读写API
【关键字】:C/C++,ini,API
【来  源】:http://blog.csdn.net/yangjundeng/archive/2007/01/15/1483594.aspx

C/C++跨平台ini文件读写API

Your Ad Here  

昨天写了个一组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    
*/

#ifndef INI_FILE_H_
#define INI_FILE_H_

#ifdef __cplusplus
extern "C"
{
#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
}; 
//end of extern "C" {
#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>

#include 
"inifile.h"

#ifdef __cplusplus
extern "C"
{
#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 ");

    
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;
}

链表的C语言实现之循环链表及双向链表:【上一篇】
Visual C++ 编译器选项:【下一篇】
【相关文章】
  • J2ME MMAPI开发移动多媒体应用
  • 使用openssl api进行加密解密
  • TSM5.2 Administrative Interface for TSM5.3
  • Win32 APIs 资源 for .net
  • 删除_desktop.ini
  • java API概述
  • EJB3 Java 持久化API来标准化Java的持久化学习笔记
  • [转]C/C++面试题大汇总
  • [转]C/C++ 笔试、面试题目大汇总(一)
  • [转]C/C++ 笔试、面试题目大汇总(二)
  • 【随机文章】
  • monoforge提供mono主机服务
  • 最全的后门制作及安装技术 四
  • prototype1.5应用--简单的表单提交验证
  • Windows 2000中隐患重重的十大“服务”
  • Solaris的启动问题及解决办法
  • VSTS 最新的文章,不少功能都是针对最新Beta3的
  • 浏览器操作记录工具Internet Macros
  • 视频播音、录音、翻唱问题集锦
  • VPN与VMware
  • Oracle认证介绍
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.