linux c 读写 ini 配置文件
.ini 文件格式如下:
[section1]
key1=value
...
keyn=value
[section2]
key1=value
...
keyn=value
代码如下:
#define _PARAM_GLOBALS_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "userlib.h"
#include "paramConfig.h"
#define SECTION_MAX_LEN 256
#define STRVALUE_MAX_LEN 256
#define LINE_CONTENT_MAX_LEN 256
//read value from .ini
void IniReadValue(char* section, char* key, char* val, const char* file)
{
FILE* fp;
int i = ;
int lineContentLen = ;
int position = ;
char lineContent[LINE_CONTENT_MAX_LEN];
bool bFoundSection = false;
bool bFoundKey = false;
fp = fopen(file, "r");
if(fp == NULL)
{
printf("%s: Opent file %s failed.\n", __FILE__, file);
return;
}
while(feof(fp) == )
{
memset(lineContent, , LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
if((lineContent[] == ';') || (lineContent[] == '\0') || (lineContent[] == '\r') || (lineContent[] == '\n'))
{
continue;
} //check section
if(strncmp(lineContent, section, strlen(section)) == )
{
bFoundSection = true;
//printf("Found section = %s\n", lineContent);
while(feof(fp) == )
{
memset(lineContent, , LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
//check key
if(strncmp(lineContent, key, strlen(key)) == )
{
bFoundKey = true;
lineContentLen = strlen(lineContent);
//find value
for(i = strlen(key); i < lineContentLen; i++)
{
if(lineContent[i] == '=')
{
position = i + ;
break;
}
}
if(i >= lineContentLen) break;
strncpy(val, lineContent + position, strlen(lineContent + position));
lineContentLen = strlen(val);
for(i = ; i < lineContentLen; i++)
{
if((lineContent[i] == '\0') || (lineContent[i] == '\r') || (lineContent[i] == '\n'))
{
val[i] = '\0';
break;
}
}
}
else if(lineContent[] == '[')
{
break;
}
}
break;
}
}
if(!bFoundSection){printf("No section = %s\n", section);}
else if(!bFoundKey){printf("No key = %s\n", key);}
fclose(fp);
} int readStringValue(const char* section, char* key, char* val, const char* file)
{
char sect[SECTION_MAX_LEN];
//printf("section = %s, key = %s, file = %s\n", section, key, file);
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
printf("%s: input parameter(s) is NULL!\n", __func__);
return READ_STR_ERR;
} memset(sect, , SECTION_MAX_LEN);
sprintf(sect, "[%s]", section);
//printf("reading value...\n");
IniReadValue(sect, key, val, file); return READ_STR_OK;
} int readIntValue(const char* section, char* key, const char* file)
{
char strValue[STRVALUE_MAX_LEN];
memset(strValue, '\0', STRVALUE_MAX_LEN);
if(readStringValue(section, key, strValue, file) != READ_STR_OK)
{
printf("%s: error", __func__);
return ;
}
return(atoi(strValue));
} void IniWriteValue(const char* section, char* key, char* val, const char* file)
{
FILE* fp;
int i = , n = , err = ;
int lineContentLen = ;
int position = ;
char lineContent[LINE_CONTENT_MAX_LEN];
char strWrite[LINE_CONTENT_MAX_LEN];
bool bFoundSection = false;
bool bFoundKey = false; memset(lineContent, '\0', LINE_CONTENT_MAX_LEN);
memset(strWrite, '\0', LINE_CONTENT_MAX_LEN);
n = sprintf(strWrite, "%s=%s\n", key, val);
fp = fopen(file, "r+");
if(fp == NULL)
{
printf("%s: Opent file %s failed.\n", __FILE__, file);
return;
}
while(feof(fp) == )
{
memset(lineContent, , LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
if((lineContent[] == ';') || (lineContent[] == '\0') || (lineContent[] == '\r') || (lineContent[] == '\n'))
{
continue;
}
//check section
if(strncmp(lineContent, section, strlen(section)) == )
{
bFoundSection = true;
while(feof(fp) == )
{
memset(lineContent, , LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
//check key
if(strncmp(lineContent, key, strlen(key)) == )
{
bFoundKey = true;
printf("%s: %s=%s\n", __func__, key, val);
fseek(fp, (-strlen(lineContent)),SEEK_CUR);
err = fputs(strWrite, fp);
if(err < ){printf("%s err.\n", __func__);}
break;
}
else if(lineContent[] == '[')
{
break;
}
}
break;
}
}
if(!bFoundSection){printf("No section = %s\n", section);}
else if(!bFoundKey){printf("No key = %s\n", key);}
fclose(fp);
} int writeStringVlaue(const char* section, char* key, char* val, const char* file)
{
char sect[SECTION_MAX_LEN];
//printf("section = %s, key = %s, file = %s\n", section, key, file);
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
printf("%s: input parameter(s) is NULL!\n", __func__);
return READ_STR_ERR;
}
memset(sect, '\0', SECTION_MAX_LEN);
sprintf(sect, "[%s]", section);
IniWriteValue(sect, key, val, file);
} int writeIntValue(const char* section, char* key, int val, const char* file)
{
char strValue[STRVALUE_MAX_LEN];
memset(strValue, '\0', STRVALUE_MAX_LEN);
sprintf(strValue, "%-4d", val); writeStringVlaue(section, key, strValue, file);
}
在 writeIntValue() 函数中 sprintf(strValue, "%-4d", val); 做了对齐及位宽处理,主要是因为避免不同的位数数据写入出现错误。目前还没想到比较好的解决方案,暂时就这样处理了。
linux c 读写 ini 配置文件的更多相关文章
- C# 读写 ini 配置文件
虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...
- [转]VB 读写ini 配置文件
转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...
- 自己写的 读写 ini 配置文件类
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...
- 引用“kernel32”读写ini配置文件
引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件 引用"kernel32"读写ini配置文件 OverView ke ...
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
C# 文件的一些基本操作 2009-07-19 来自:博客园 字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...
- C#操作读写INI配置文件
一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...
- c#读写ini配置文件示例
虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧 其他人写的都是调用非托管kernel32.dll.我也用过 ...
- WritePrivateProfileString等读写.ini配置文件
配置文件中经常用到ini文件,在VC中其函数分别为: 写入.ini文件: BOOL WritePrivateProfileString( LPCTSTR lpAppName, // INI文件中的一个 ...
- C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()
转载: 1.https://blog.csdn.net/fengbingchun/article/details/6075716 2. 转自:http://hi.baidu.com/andywangc ...
随机推荐
- 【Material Design视觉设计语言】开篇
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...
- spring boot打包后在tomcat无法访问静态资源问题
我的spring boot项目中前端页面的资源引用 我的静态文件夹是 我的application.yml中资源路径配置了 同时我在WebMvcConfig中配置了addResourceHandlers ...
- 巧用redis位图存储亿级数据与访问 - 简书
原文:巧用redis位图存储亿级数据与访问 - 简书 业务背景 现有一个业务需求,需要从一批很大的用户活跃数据(2亿+)中判断用户是否是活跃用户.由于此数据是基于用户的各种行为日志清洗才能得到,数据部 ...
- vuex的简单例子和vue model组件
好久没用过vuex了,vuex官方示例的计算器counter是用的webpack打包了单文件组件,不方便回顾,今天把代码改成了html引人的方式,方便回顾 <!DOCTYPE html> ...
- 【45.61%】【codeforces 701D】As Fast As Possible
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 获取当前div中的所有div的个数和每一个div的ID and 根据屏幕分辨率计算高度
var tabs_Count=document.getElementById("tabs-panels").getElementsByTagName("div" ...
- STL序列容器之vector
一,vector容器简介 1.vector容器的原理 vector是将元素置于一个动态数组中加以管理的容器. 2.vector容器的特点 vector容器可以随机存取元素,支持索引存取(即用数组下标的 ...
- js延迟加载
setTimeout('yourFunction()',5000); 5秒后执行yourFunction(),只执行一次 setInterval('yourFunction()',5000); 每隔5 ...
- 2-2 Consul注册注销流程
铺垫,创建健康检查方法,Consul服务器隔一段时间请求一下webapi里的一个方法,如果这个方法没有问题,则证明这个webapi还在正常工作,这个webapi提供的服务就存在.如果方法没有返回,或者 ...
- 我所理解的设计模式(C++实现)——观察者模式(Observer Pattern)
概述: 近期中国股市起起伏伏,当然了起伏就用商机,小明发现商机后果断想入市,买入了中国证券,他想在电脑client上,网页上,手机上,iPad上都能够查看到该证券的实时行情,这样的情况下我们应该怎么设 ...