linux读写配置文件

在Linux中实现类似windows中获取配置文件的函数GetProfileString。在读取配置文件时,window环境下,有GetProfileString函数,而Linux下则没有。网上找了⼀个能
 实现其功能的函数,原理很简单。如下所示,基本思想是捉住配置文件中用“[]”标记的段没有“=”,而非“[]”段有“=”这⼀特征,先找section段,再找键,得到对应的值。
 配置文件示例
 [section1]
 age = 12
 name = Wang
 [section2]
 age = 13
 name = Zhang

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

using namespace std;

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] = {0};
    string line2;
    string::size_type 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); //该行最后一个空格之后开始的为所要的item_value
            found = true;
        }
    }
    mystream.close();
    return 0;
}

自己加了写配置文件的函数,原理差不多, 找到相应的键值进行替换,在移动文件指针,将修改后值写入。
int SetProfileString(string file_name, string section_name, string item_name,
        string &item_value)
{
    fstream mystream ;
    mystream.open(file_name.c_str());

    if (!mystream)
    {
        cout << "Error " << endl;
        return -1;
    }

    char line[30] = {0};
    string line2;
    string::size_type 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); //该行最后一个空格之后开始的为所要的item_value
            line2.replace(return_of_find+1, item_value.size(), item_value);
            //mystream<<line2.c_str();
            int len = line2.size();
            mystream.seekp(-(len), ios::cur);  //移动文件指针
            mystream<<line2<<"\n";
            found = true;
        }
    }
    mystream.close();

    return 0;
}



                
Linux 中,读取配置文件通常使用以下步骤: 1. 打开配置文件:使用文件操作函数(如 fopen)打开要读取配置文件配置文件通常位于文本格式,可以使用文本编辑器打开。 2. 读取配置项:使用文件操作函数(如 fgets 或 getline)逐行读取配置文件内容。可以根据需要使用字符串处理函数(如 strtok 或 sscanf)解析每行的配置项。 3. 处理配置项:根据配置文件的格式和内容,将读取到的配置项进行相应的处理。例如,可以将配置项的键值对存储到变量中,或者执行相应的操作。 4. 关闭配置文件:使用文件操作函数(如 fclose)关闭已打开的配置文件,释放资源。 以下是一个简单的示例代码,用于演示如何读取一个名为 "config.txt" 的配置文件: ```c #include <stdio.h> int main() { FILE *file = fopen("config.txt", "r"); if (file == NULL) { perror("Failed to open config file"); return 1; } char line[256]; while (fgets(line, sizeof(line), file)) { // 处理每行配置项 // 例如,可以使用 strtok 函数解析键值对 char *key = strtok(line, "="); char *value = strtok(NULL, "="); if (key && value) { printf("Key: %s, Value: %s\n", key, value); } } fclose(file); return 0; } ``` 在上述示例中,我们打开了一个名为 "config.txt" 的配置文件,并逐行读取其中的配置项。使用 strtok 函数将每行的键值对分隔开来,并进行处理。最后关闭配置文件。 请注意,这只是一个简单的示例,实际的配置文件读取可能涉及更复杂的逻辑和错误处理。具体的实现方式可能因编程语言和需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值