.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 配置文件的更多相关文章

  1. C# 读写 ini 配置文件

    虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...

  2. [转]VB 读写ini 配置文件

    转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...

  3. 自己写的 读写 ini 配置文件类

    /// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...

  4. 引用“kernel32”读写ini配置文件

    引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件  引用"kernel32"读写ini配置文件 OverView ke ...

  5. C# 文件的一些基本操作(转)//用C#读写ini配置文件

    C# 文件的一些基本操作 2009-07-19  来自:博客园  字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...

  6. C#操作读写INI配置文件

    一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...

  7. c#读写ini配置文件示例

    虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧     其他人写的都是调用非托管kernel32.dll.我也用过 ...

  8. WritePrivateProfileString等读写.ini配置文件

    配置文件中经常用到ini文件,在VC中其函数分别为: 写入.ini文件: BOOL WritePrivateProfileString( LPCTSTR lpAppName, // INI文件中的一个 ...

  9. C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()

    转载: 1.https://blog.csdn.net/fengbingchun/article/details/6075716 2. 转自:http://hi.baidu.com/andywangc ...

随机推荐

  1. 免费的 C/C++ 编译&解释 器列表

    摘自<C++编程网>,详细介绍请参考http://www.cpp-prog.com/2009/0520/118.html MicrosoftVisual C++ 2008 Express  ...

  2. CocoaPods详解之(三)----制作篇

    CocoaPods详解之----制作篇 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/20067595 转载请注明出处 ...

  3. scala 通过apply创建类的对象

    package cn.scala_base.oop.scalaobject; class Boy(name: String) { private var age: Int = 0; println(n ...

  4. 关于java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccessible

    <span style="font-size:18px;"> java.lang.NoSuchMethodError: org.springframework.util ...

  5. linux 网卡配置文件详解

    配置文件位置:/etc/sysconfig/network-scripts/ifcfg-eth0 1. DEVICE=eth0 网卡的名字 2. HWADDR=00:0c:29:90:89:d9 HW ...

  6. linux系统下信号具体解释2

    信号是UNIX 系统所使用的进程通信方法中,最古老的一种.信号不但能从内核发往一个进程,也能从一个进程发往还有一个进程.比如,用户在后台启动了一个要运行较长时间的程序,假设想中断其运行,能够用kill ...

  7. win10 支持默认把触摸提升鼠标事件 打开 Pointer 消息

    原文:win10 支持默认把触摸提升鼠标事件 打开 Pointer 消息 在 WPF 经常需要重写一套触摸事件,没有UWP的Pointer那么好用. 如果一直都觉得 WPF 的触摸做的不好,或想解决 ...

  8. Linux 下的任务管理 —— ps、top

    ps:report a snapshot of the current processes. ps 命令支持三种使用的语法格式 UNIX 风格,选项可以组合在一起,并且选项前必须有"-&qu ...

  9. Hibernate——(1)Hibernate入门

    一.Hibernate简介 1.Hibernate是一款ORM框架,Object Relation Mapping 对象关系映射. 2.可以将DB映射成Object,这样程序只要对Object对象进行 ...

  10. 浅谈java枚举类

    一.什么情况下使用枚举类? 有的时候一个类的对象是有限且固定的,这种情况下我们使用枚举类就比较方便? 二.为什么不用静态常量来替代枚举类呢? public static final int SEASO ...