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;
}


目录
相关文章
|
10天前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
108 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
10天前
|
JSON 安全 Linux
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
60 1
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
|
10天前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
164 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
12天前
|
NoSQL IDE MongoDB
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
79 1
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
12天前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
44 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
12天前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
84 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
12天前
|
安全 Linux iOS开发
Tenable Nessus 10.10 (macOS, Linux, Windows) - 漏洞评估解决方案
Tenable Nessus 10.10 (macOS, Linux, Windows) - 漏洞评估解决方案
106 0
Tenable Nessus 10.10 (macOS, Linux, Windows) - 漏洞评估解决方案
|
10天前
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
39 0
|
Ubuntu Linux 虚拟化
安装Windows Linux 子系统的方法:适用于windows 11 版本
本文提供了在Windows 11系统上安装Linux子系统(WSL)的详细步骤,包括启用子系统和虚拟化功能、从Microsoft Store安装Linux发行版、设置WSL默认版本、安装WSL2补丁,以及完成Ubuntu的首次安装设置。
4241 2
|
Linux Windows Ubuntu
Windows 使用 Linux 子系统,轻轻松松安装多个linux
Windows 使用 Linux 子系统,轻轻松松安装多个linux
1369 0
Windows 使用 Linux 子系统,轻轻松松安装多个linux