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 ...
随机推荐
- 【codeforces 782A】Andryusha and Socks
[题目链接]:http://codeforces.com/contest/782/problem/A [题意] 如果手套没有成一双,那么其中的一只就会被放在桌子上; 问你桌子上手套的只数最多的时候有几 ...
- 课后作业11--使用SQL语句创建一个数据库
use master if db_id ('test') is not null--判断test数据库是否存在 drop database [test]--如果存在 删除test go--完成查找删除 ...
- Docker Xshell
Windows安装Docker Xshell无法连接虚拟机解决方案 DOCKER windows安装 6.1 下载地址 6.2 用FTP工具上传tar包 6.3 安装 6.4 查看镜像 6.5 运行 ...
- Android之assets资源目录的各种操作
第一种方法: String path = file:///android_asset/文件名; 第二种方法: InputStream abpath = getClass() ...
- <meta http-equiv="refresh" content="0; url=">什么意思?
原文:<meta http-equiv="refresh" content="0; url=">什么意思? 页面定期刷新,如果加url的,则会重新定 ...
- Tomcat 学习总结
1. 下载地址 Eclipse: http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/photo ...
- Delphi 中的字符串(还解释了shortstring)good
一.Delphi 2009 之前的字符串(不支持 Unicode): Delphi 2009 之前的字符串分为 3 种:ShortString.AnsiString.WideString. [Shor ...
- yii2.0复选框默认选中
<?php $model->node = array('0','2') ;?> <? echo $form->field($model,'node')->che ...
- Fast exit from dram self-refresh
Embodiments of the invention describe a dynamic random access memory (DRAM) device that may abort a ...
- [C++] 反编译器
各种开源的decompiler都不太好用,眼下最好的反编译器是IDA pro. 尽管是收费的,只是破解版非常好找. 我试过5.5版本号的,还不错. 我把windows notepad进行了反编译,多少 ...