20、在Linux中实现类似windows中获取配置文件的函数GetProfileString

简介: 在读取配置文件时,window环境下,有GetProfileString函数,而Linux下则没有。我写了一个能实现其功能的函数,如下所示,基本思想是捉住配置文件中用“[]”标记的段没有“=”,而非“[]”段有“=”这一特征,先找section段,再找键,得到对应的值。

在读取配置文件时,window环境下,有GetProfileString函数,而Linux下则没有。我写了一个能实现其功能的函数,如下所示,基本思想是捉住配置文件中用“[]”标记的段没有“=”,而非“[]”段有“=”这一特征,先找section段,再找键,得到对应的值。不当之处,欢迎批评指正。

配置文件示例

[section1]
age = 12
name = edward
[section2]
age = 13
name = lewis

代码示例

#include "cstdio"
#include "iostream"
#include "string"
#include "fstream"


using namespace std;

const int OP_SUCCESS = 0;
const int OP_FAILED = -1;

int GetProfileString(string file_name, string section_name, string item_name, string &item_value)
{
	ifstream mystream;
	mystream.open(file_name.c_str(), ios::in);
	if (!mystream)
	{
		cout << "Error " << endl;
		return -1;
	}

	char line[30];
	string line2;
	size_t return_of_find;
	bool found = false;
	while(mystream.getline(line, 30) && !found) //默认行不会超过30个字符
	{
		line2 = line;
		return_of_find = line2.find(section_name);
		if (string::npos == return_of_find)
		{
			continue; //没找到section项,则继续下一行读取
		}

		//找到了,则执行第二步,寻找相应的键值,关键是不能跨越多段
		while(mystream.getline(line, 30) && !found)
		{
			line2 = line;
			string equal_flag = "=";
			return_of_find = line2.find(equal_flag);

			if (string::npos == return_of_find)
			{
				//说明已经跨越了多段,目标寻找失败
				return -1;
			}

			//还在当前段中
			return_of_find = line2.find(item_name);
			if (string::npos == return_of_find)
			{
				//没有找到
				continue;
			}
			//找到了
			return_of_find = line2.rfind(" "); //要求配置文件=两边要有空格
			item_value = line2.substr(return_of_find + 1);
			found = true;
			if (' ' == item_value[0])
			{
				item_value = item_value.substr(1);
			}
		}
	}
	mystream.close();
	return 0;
}
int main(int argc, char* argv[])
{
	string file_name, section_name, item_name, item_value;
	file_name = "F:\\test.txt";
	section_name = "section2";
	item_name = "age";
	GetProfileString(file_name, section_name, item_name, item_value);
	cout << item_value << endl;
	return 0;
}


目录
相关文章
|
5天前
|
iOS开发 MacOS Windows
|
5天前
|
Linux
linux中wait与waitpid函数使用场景及扩展
linux中wait与waitpid函数使用场景及扩展
|
5天前
|
NoSQL Linux Redis
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
29 0
|
5天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
13 0
|
5天前
|
消息中间件 Unix Linux
【探索Linux】P.14(进程间通信 | 匿名管道 | |进程池 | pipe() 函数 | mkfifo() 函数)
【探索Linux】P.14(进程间通信 | 匿名管道 | |进程池 | pipe() 函数 | mkfifo() 函数)
26 0
|
5天前
|
Linux Shell
Linux中system函数
Linux中system函数
9 0
|
5天前
|
Linux Shell 调度
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
|
5天前
|
前端开发 Linux iOS开发
【Flutter前端技术开发专栏】Flutter在桌面应用(Windows/macOS/Linux)的开发实践
【4月更文挑战第30天】Flutter扩展至桌面应用开发,允许开发者用同一代码库构建Windows、macOS和Linux应用,提高效率并保持平台一致性。创建桌面应用需指定目标平台,如`flutter create -t windows my_desktop_app`。开发中注意UI适配、性能优化、系统交互及测试部署。UI适配利用布局组件和`MediaQuery`,性能优化借助`PerformanceLogging`、`Isolate`和`compute`。
【Flutter前端技术开发专栏】Flutter在桌面应用(Windows/macOS/Linux)的开发实践