C语言读取配置文件
自从大学学完C之后,就再也没用过它了,
在网上找代码,七拼八凑之后,终于成形~~勉强能用,不喜勿喷,^_^!
int GetValue(const wchar_t *key, wchar_t *value)
{
FILE* fpcfg = NULL;
wchar_t var[], val[];//key,value
wchar_t linebuf[];
wchar_t* ptr1 = NULL;
wchar_t* ptr2 = NULL;
wchar_t* delimiter = NULL;
int k = ; assert(key != NULL && value != NULL); //testConfig.ini中的内容为:
//key1 = value1
//key2 = value2
fpcfg = fopen("testConfig.ini", "rt");
if(fpcfg == NULL)
{
return -;
} while(fgetws(linebuf, sizeof(linebuf), fpcfg) != NULL)
{
//ignore annotation line
if(linebuf[]=='#' || linebuf[]==';')
{
continue;
} //ignore empty line
ptr1 = linebuf;
while(*ptr1==0x20 || *ptr1=='\t')
ptr1++;
if(!*ptr1 || *ptr1=='\n')
{
continue;
}
//search "="
ptr2 = ptr1;
while(*ptr2 && *ptr2!='=')
{
ptr2++;
}
delimiter = *ptr2?ptr2:NULL;
//if current line start with "=", continue next line
if(!delimiter || delimiter==ptr1)
{
continue;
}
//*delimiter = '\0';
ptr2 = delimiter-;
//ignore blank before "="
while(*ptr2==0x20 || *ptr2=='\t')
{ ptr2--;}
//check key length
k = ptr2-ptr1+;
if(k>(int)sizeof(var)-)
{
//The key name is out of length."
continue;
}
//save key name
ptr2 = var;
while(k--)
{
*ptr2++ = *ptr1++;
}
*ptr2 = '\0'; //locate value's start point(may be '\0')
ptr1 = delimiter+;
//ignore blank after "="
while(*ptr1==0x20 || *ptr1=='\t')
{
ptr1++;
}
//set ptr2 point to line end
ptr2 = linebuf;
while(*ptr2)
{ptr2++;}
ptr2--;
if(*ptr2=='\n')
{
*ptr2-- = '\0';
}
//ignore blank line end
//if value is empty,ptr2 --> = , ptr1>ptr2
while(*ptr2==0x20 || *ptr2=='\t' )
{
ptr2--;
}
//check value length
k = ptr2-ptr1+;
if(k>(int)sizeof(val)-)
{
//The value is out of length"
continue;
}
//save value
ptr2 = val; while(k-->)
{
*ptr2++ = *ptr1++;
} *ptr2 = '\0'; if(wcsncmp(var,key, wcslen(var) > wcslen(key) ? wcslen(var) : wcslen(key))==){
wcsncpy(value,val, wcslen(val));
return ;
}
}
fclose(fpcfg);
fpcfg = NULL; return -;
}
恩,再来个main方法测试下:
int _tmain(int argc, wchar_t* argv[])
{
wchar_t value[] = {}; wchar_t keyParam[] = L"ip";
wchar_t *temp = (wchar_t *)malloc(sizeof(wchar_t) * ); if(temp != NULL){
memset(temp, 0x00, sizeof(wchar_t) * );
}else{
return -;
} if( == GetValue(keyParam, temp)){
wcsncpy(value, temp, wcslen(temp));
wprintf(L"Found: %s\n", value);
}else{
wprintf(L"Not Found!!\n");
} if(temp != NULL)
{
free(temp);
temp = NULL;
}
return ;
}
好吧,这确实是个很丑陋的版本,仅供自己留着备用。
点这里下载
C语言读取配置文件的更多相关文章
- C语言 读取配置文件
配置文件截图: 读取结果截图: 代码转自:http://www.tuicool.com/articles/Zb2iIn 附代码: // ReadConfig.cpp : 定义控制台应用程序的入口点. ...
- Go语言读取各种配置文件
配置文件结构体 config.go package config type System struct { Mode string `mapstructure:"mode" jso ...
- Windows 服务多语言化时读取配置文件失败的问题。
在Installer中,按一般读取配置文件的方法(ConfigurationManager.AppSettings["CultureName"])读取不到内容. 可以这样读取: v ...
- Python语言的configparser模块便捷的读取配置文件内容
配置文件是在写脚本过程中经常会用到的,所以读取配置文件的模块configparser也非常重要,但是很简单. 首先我们的配置文件内容为: 这样的配置文件,[]里面的内容叫section,[]下面的内容 ...
- 利用java反射机制 读取配置文件 实现动态类载入以及动态类型转换
作者:54dabang 在spring的学习过程之中,我们能够看出通过配置文件来动态管理bean对象的优点(松耦合 能够让零散部分组成一个总体,而这些总体并不在意之间彼此的细节,从而达到了真正的物理上 ...
- 依据不同的操作系统读取配置文件/java读取属性文件代码
package cn.com.css.common.util; /** * @brief OSEnum.java 操作系统的枚举 * @attention * @author 涂作权 * @d ...
- java中读取配置文件ResourceBundle和Properties两种方式比较
今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...
- ResourceBundle与Properties读取配置文件
ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...
- ResourceBundle和properties 读取配置文件区别
java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Properties同时还能用来写文 ...
随机推荐
- 红帽子系统链接加vm15秘钥一份
vm15秘钥:YZ718-4REEQ-08DHQ-JNYQC-ZQRD0 红帽子系统下载链接:http://www.ddooo.com/softdown/60964.htm
- python 发qq邮件
import smtplibfrom email.mime.text import MIMETextmsg_from = '979477675@qq.com' # 发送方邮箱passwd = 'irg ...
- 消息中间件(一)MQ详解及四大MQ比较
一.消息中间件相关知识 1.概述 消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流 ...
- 用Python玩转数据——第五周数据统计和可视化
一.数据获取 1.本地数据 with 语句,pd.read_csv('data.csv') 2.网站上数据 2.1 直接获取网页源码,在用正则表达式进行删选 2.2 API接口获取---以豆瓣为例 i ...
- 判断easyUI tree 节点复选框是否被选中的方法。将选中的节点高亮显示的方法
在datagrid tree中如何判断某个节点的复选框是否被选中? 我们可以通过HTML结构去分析: 1.节点未选中前 2.节点选中后 所以节点被选中前和选中后,html中的class类是用区分的. ...
- Linux CentOS7系统中mysql8安装配置
mysql是世界上最流行的关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司所有.今天我将记录一下如何在Linux centos7系统上安装和配置MySQL. 目录 环境准 ...
- IP等级
IP是Ingress Protection的缩写,IP等级是针对电气设备外壳对异物侵入的防护等级,来源是国际电工委员会的标准IEC 60529,这个标准在2004年也被采用为美国国家标准. 在这个标 ...
- CentOS 安装Scrapy
本文python版本是python3.5.3,关于升级python和安装pip请到:http://www.cnblogs.com/technologylife/p/6242115.html 安装相关包 ...
- (转)Python科学计算之Pandas详解,pythonpandas
https://www.cnblogs.com/linux-wangkun/p/5903380.html-------pandas 学习(1): pandas 数据结构之Series https:// ...
- habase 报错 ERROR: Can't get master address from ZooKeeper; znode data == null
方法一:查看日志报SessionExpiredException: KeeperErrorCode = Session expired for /hbase/master 所以是hbase 和 zoo ...