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同时还能用来写文 ...
随机推荐
- Open vSwitch 2.9.2 创建 RPM 安装包
1.安装依赖环境 yum install gcc make python-devel openssl-devel graphviz autoconf automake rpm-build redhat ...
- jzoj4424
20%:暴力枚舉每一條邊有沒有被選到,然後使用并查集判斷聯通性 這樣子有20分,但是我考試寫掛了所以1分也沒有 100%:這道題2000的數據範圍,使用指數級搜索會tle,需要更加好的方法 這道題中, ...
- Exploit之初识Linux下缓冲区溢出
本文的目的不是为了介绍如何进行恶意的破坏性活动,而是为了教会你如何去防御此类破坏性活动,以帮助你扩大知识范围,完善自己的技能,如有读者运用本文所学技术从事破坏性活动,本人概不负责. 0×01 前言 1 ...
- vue 学前班002(创建一个实例)
创建一个Vue实例 我们之前说过,Vue是数据驱动视图的,只要model(模型层)的数据改变了,Vue就会帮我们改变视图上显示的内容,不用我们自己去动手写代码更新.那么,它是怎么做到的,我们的代码要怎 ...
- FlexMonkey实战
文章来源:http://www.cnblogs.com/raol/p/flexmonkey.html 我的有道云笔记:http://note.youdao.com/share/?id=22b79669 ...
- 了解ORACLE培训OCA-OCP-OCM课程表
了解ORACLE培训OCA-OCP-OCM课程表考试号: OCA 1Z0-007$125 Oracle Database 10g:SQL Fundamentals 本课程培养学生必要的SQ ...
- UIDevice currentDevice model possible values
NOTE: The below code may not contain all device's string, I'm with other guys are maintaining the sa ...
- (转)python 之路,200行Python代码写了个打飞机游戏!
原文:https://www.cnblogs.com/alex3714/p/7966656.html
- python strip()函数的用法
函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中 ...
- 《构建之法》阅读笔记06-项目经理PM
软件团队里除了能写代码.测试代码和画图做设计的成员,还有一类角色,不做上面这些事情但也很重要,我们叫他们项目经理--PM. PM 的M 就是 Manager: P有这几种: Product Manag ...