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 ...
随机推荐
- _Decoder_Interface_init xxxxxx in amrFileCodec.o
Undefined symbols for architecture arm64: "_Decoder_Interface_init", referenced from: Deco ...
- scala 伴生对象与伴生类
package cn.scala_base.oop.scalaobject import java.security.cert.Extension /** * object的构造器必须是无参的,且且构 ...
- R 语言文件读写
1. working directory:工作目录 > getwd() > setwd("C:/data") # 设定当前工作目录 2. 读取格式化的 table &g ...
- exec 与 open 打开进程
1 exec ?-keepnewline ?-ignorestderr args(?号后面表示可以跟的参数) 这个东西一旦执行 , 没有执行完毕父进程会处于等待中 使用一个或多个子进程运行 由arg ...
- less - 循环 loop
.avatar-loop(@n, @i:1, @level) when (@i <= @n) { &:nth-child(@{level}) .item.item-@{i} { .ava ...
- Dictionary(数据字典)
数据字典:Dictionary对象用于在结对的名称/值中存储信息(等同于键和项目),其可作为传参使用. C# Dictionary字典类的使用方法 //定义字典 Dictionary<strin ...
- FontAwesome 图标
FontAwesome 图标 前言 FontAwesome 大家都不陌生,精美的图标,出现在各式各样的网页中.最近在做 Windows Forms 应用程序,要求美观,就想能不能把 FontAweso ...
- 参数的范数正则/惩罚(parameter norm penalties)
1. L2 范数 J~(w;X,y)=J(w;X,y)+α2wTw J 表示的是原始的目标函数,J~ 则是二范数约束后的新的目标函数. 则根据梯度下降算法有: ∇wJ~=∇wJ+αw w←w−ϵ∇wJ ...
- C++安全异常std:auto_ptr
auto_ptr它是C++标准库(<utility>)为了一个智能指针类模板来解决资源泄漏所提供的问题(注意:这只是一个简单的智能指针) auto_ptr在事实原则的实现RAII,对资源的 ...
- HDU 3360 National Treasures 奇偶匹配的最低点覆盖
标题来源:pid=3360">HDU 3360 National Treasures 意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 ...