设为首页 加入收藏

TOP

C读写配置文件(一)
2015-01-22 21:37:18 来源: 作者: 【 】 浏览:182
Tags:读写 配置 文件

在项目开发中,经常需要读取应用配置文件的初始化参数,在应用启前进行一些初始化设置。比如:Eclipse,参数项包含主题、字体大小、颜色、Jdk安装位置、自动提示等。Eclispe配置的文件格式是以键值对的方式存储的,即:key=value的形式,下面是Eclipse部份设置参数:

/instance/org.eclipse.jdt.ui/useQuickDiffPrefPage=true
/instance/org.eclipse.jdt.ui/content_assist_proposals_foreground=0,0,0
/instance/org.eclipse.egit.core/GitRepositoriesView.GitDirectories=/Users/yangxin/Downloads/kakaolink-android/.git\:/Users/yangxin/Documents/workspace_web/tfyj/.git\:
/instance/org.eclipse.wst.jsdt.ui/fontPropagated=true
/instance/org.eclipse.debug.core/org.eclipse.debug.core.USE_STEP_FILTERS=true
/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
/instance/org.eclipse.ui.workbench/org.eclipse.jface.textfont=1|Monaco|14.0|0|COCOA|1|Monaco;
@org.eclipse.jdt.ui=3.8.2.v20130107-165834
/instance/org.eclipse.cdt.ui/spelling_locale_initialized=true
从Eclipse配置文件中可以看出,都是以 xxxx=xxxxx 如最后一个配置项,key为/instance/org.eclipse.cdt.ui/spelling_locale_initialized,值为:true。在项目开发当中的也经常采用这种方式,笔者参考了Java的java.util.Properties类,设计了一个C的配置文件读写接口,供大家学习和使用。

1、定义接口头文件(Properties.h)

//
//  Properties.h
//  读写配置文件
//
//  Created by 杨信 on 14-4-24.
//  Copyright (c) 2014年 yangxin. All rights reserved.
//

#ifndef _______Properties_h
#define _______Properties_h

#ifdef _cplusplus
extern "C" {
#endif
    
    // 初始化环境,成功返回0,失败返回非0值
    int init(const char *filepath,void **handle);
    
    // 根据KEY获取值,找到返回0,如果未找到返回非0值
    int getValue(void *handle, const char *key, char *value);
    
    // 修改key对应的属性值,修改成功返回0,失败返回非0值
    int setValue(void *handle, const char *key, const char *value);
    
    // 添加一个属性,添加成功返回0,失败返回非0值
    int add(void *handle, const char *key, const char *value);
    
    // 删除一个属性,删除成功返回0,失败返回非0值
    int del(void *handle, const char *key);
    
    // 获取属性文件中所有的key,获取成功返回0,失败返回非0值
    int getKeys(void *handle, char ***keys, int *keyscount);
    
    // 释放所有key的内存空间,成功返回0,失败返回非0值
    int free_keys(char ***keys,int *keyscount);
    
    // 获取属性文件中所有的值,成功返回0,失败返回非0值
    int getValues(void *handle, char ***values, int *valuescount);
    
    // 释放所有value的内存空间,成功返回0,失败返回非0值
    int free_values(char ***values, int *valuescount);
    
    // 获取属性数量,成功返回0,失败返回非0值
    int getCount(void *handle, int *count);
    
    // 释放环境资源,成功返回0,失败返回非0值
    int release(void **handle);
    
    
#ifdef _cplusplus
}
#endif

#endif

2、实现头文件的接口(Properteis.c)

//
//  Properties.c
//  读写配置文件
//
//  Created by 杨信 on 14-4-24.
//  Copyright (c) 2014年 yangxin. All rights reserved.
//

#include 
  
   
#include 
   
     #include 
    
      #include "Properties.h" #define KEY_SIZE 128 // key缓冲区大小 #define VALUE_SIZE 128 // value缓冲区大小 #define LINE_BUF_SIZE 256 // 读取配置文件中每一行的缓冲区大小 typedef struct Properties { char *key; char *value; struct Properties *pNext; }Properties; typedef struct PROPS_HANDLE { Properties *pHead; // 属性链表头节点 char *filepath; // 属性文件路径 }PROPS_HANDLE; static int createPropsNode(Properties **props); // 创建一个节点 static int trimeSpace(const char *src,char *dest); // 去空格 static int saveConfig(const char *filepath,Properties *head); // 将修改或保存后的配置项保存到文件 // 初始化环境,成功返回0,失败返回非0值 int init(const char *filepath,void **handle) { int ret = 0; FILE *fp = NULL; Properties *pHead = NULL,*pCurrent = NULL, *pMalloc = NULL; PROPS_HANDLE *ph = NULL; char line[LINE_BUF_SIZE]; // 存放读取每一行的缓冲区 char keybuff[KEY_SIZE] = { 0 }; // 存放key的缓冲区 char valuebuff[VALUE_SIZE] = { 0 }; // 存放value的缓
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言之预处理 下一篇C语言结构体点滴

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: