解析文件以将变量分配给结构。 C ++

时间:2019-05-24 02:21:39

标签: c++ file parsing

这是我的代码,我真的很想知道我还有多远。

bool characterGetCFG(char typeOverLoad, string var_name, string full_text)
{
    int pos = full_text.find(var_name) + 1;
    if (full_text.at(pos) == *"t") {
        cout << "returned " << full_text.at(pos) << "\n";
        return true;
}
    else 
    {
        return false;
    }
}

int characterGetCFG(int typeOverLoad, string var_name, string full_text)
{
    int pos = full_text.find(var_name) + var_name.length() + 1;
    cout << "returned " << full_text.at(pos) << "\n";
    return full_text.at(pos);
}

features setconfig(features featstruct)
{
    features config; //BOOL/BOOL/BOOL/BOOL/INT[2]/BOOL/INT
    ifstream file;
    file.open("config.cfg");
    if (!file.is_open()) {
        exit(-1);
    }
    string raw;
    while (file.good()) {
        file >> raw;
    }
    for (int x = 0; x < 11; x++) {
        switch (x) {
        case 1:
            config.var1 = characterGetCFG('t', "var1", raw);
        case 2:
            config.var2 = characterGetCFG('t', "var2", raw);
        case 3:
            config.var3 = characterGetCFG('t', "var3", raw);
        case 4:
            config.var4 = characterGetCFG('t', "var4", raw);
        case 5:
            config.var5[1] = characterGetCFG(136, "var51", raw); //min
        case 6:
            config.var6[2] = characterGetCFG(542, "var62", raw); //max
        case 7:
            config.var7 = characterGetCFG('t', "var7", raw);
        case 8:
            config.var8 = characterGetCFG('t', "var8", raw);
        case 9:
            config.var9 = characterGetCFG('t', "var9", raw);
        case 10:
            config.var10 = characterGetCFG('t', "var10", raw);
        case 11:
            config.var11 = characterGetCFG('t', "var11", raw);
        }
    }
    file.close();
    return config;
}

这是我正在使用的text / cfg文件

var1=false
var2=false
var3=true
var4=false
var51=2
var62=7
var7=true
var8=t
var9=true
var10=true
var11=true

当我打印要返回到控制台的内容时,这就是我要返回的内容

enter image description here

如您所见,它并不能反映我在循环中进行了多少次迭代,所以到了最后,我很困惑。

0 个答案:

没有答案