按key获取 txt文件的value

1. 问题描述

config.txt文件内容如下:
expo: [10000]
gain: [2.2]
width: [1280]
height: [1024]
size: [3]

要求:给定一个key(如expo),输出float类型的value(10000)。
参考网址:查找文本文件中的关键字

2. 思路

  1. 获取.txt文件的长度,即字节数length。
  2. 开劈一个length+1大小的数组char *text,保存文件数据
  3. 把文件的内容写到text数组中
  4. 进行模式串匹配,并返回keyword对应的value在text数组的首尾位置
  5. 通过value在text数组的首尾位置,为value赋值
  6. value:char型数组转换为float类型,返回float类型的Fvalue

3. 代码实现

#include <iostream>
#include <fstream>


//获取文件的长度
int GetFileLength(std::ifstream &inputFile)
{
    //保存文件当前位置  
    std::streampos pos = inputFile.tellg();

    //定位到文件尾  
    inputFile.seekg(0, std::ios::end);

    //返回文件尾的偏移量,即文件的大小  
    int length = inputFile.tellg();

    //返回到文件先前的位置  
    inputFile.seekg(pos);

    return length;
}


//模式串匹配,得到keyword对应的value在text数组中的首尾位置
int IndexOf(const char *text, int &textSize, const char *match, int matchSize, int &start, int &end)
{
    int index = 0;//keyword在text中的位置

    for (int i = 0; i <= textSize - matchSize; ++i)
    {
        int j = 0;
        while (j < matchSize && match[j] == text[i + j])
        {
            ++j;
        }

        //所有的字符都与文本中的一致,则匹配成功
        if (j == matchSize)
        {
            index = i;//找到keyword在text中的位置

            //寻找value在text数组中的首尾位置
            int k = index;
            while (text[k] != '[')
            {
                k++;
                if (text[k] == '[')
                {
                    start = k + 1;
                    break;
                }
            }

            int t = start;
            while (text[t] != ']')
            {
                t++;
                if (text[t] == ']')
                {
                    end = t - 1;
                    break;
                }
            }
            return 0;
        }
    }

    //匹配失败
    return -1;
}


//实现字符串匹配
float getValue(const char *fileName, const char *keyWord)
{
    //以只读方式,打开文件fileName
    std::ifstream inputFile(fileName);
    if (!inputFile)
    {
        //打开文件失败
        std::cout << "error: unable to open input file: " << fileName << std::endl;
        return NULL;
    }

    //获得文件的长度,即字节数,并开劈一个同样大小的数组保存文件数据
    int length = GetFileLength(inputFile);
    char *text = new char[length + 1];

    //把文件的内容写到text数组中
    inputFile.read(text, length);
    inputFile.close();
    text[length] = '\0';

    //进行模式串匹配,并返回keyword在text数组的位置
    int start = 0;
    int end = 0;
    IndexOf(text, length, keyWord, strlen(keyWord), start, end);
    int valueSize = end - start + 1;

    char *value = new char[valueSize];
    for (int i = 0; i < valueSize; i++)
    {
        value[i] = text[start + i];
    }


    float Fvalue;
    Fvalue = atof(value);//char型数组转换为float类型


    delete[]text;
    delete[]value;
    return Fvalue;
}


int main()
{
    const char *fileName = "F:/按key获取txt文件的value/config.txt";
    const char *keyWord = "size";
    float value = getValue(fileName, keyWord);
    std::cout << value << std::endl;

    system("pause");
    return 0;
}

运行结果:
3
请按任意键继续…

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值