在文本文件中提取不同格式的数据

时间:2014-03-10 14:28:45

标签: c++ fstream getline

我在阅读数据时遇到了问题,过去一小时我一直在研究它,但没有更好的结论。下面粘贴的是我的代码和我正在阅读的文件的片段。文件正确打开,结构声明也在下面显示。我有信心解决方案很简单,因为我在这个项目上工作的时间过长,所以我已经过度思考了,但这是我的解释。

数据文件正确打开,所有数据都被读入适当的位置,直到while(infile>> temp.startDistance .....)部分。此时我的目标是在读取随机僵尸的数量后读取每轮中明确说明的僵尸(参见格式的数据文件)。在以前读取数据的情况下,我使用getline,而不是从getline接收的字符串的c字符串版本上的sscanf来提取适当的数据。由于显式僵尸的格式只是只用空格分隔数据,我想使用>> operater提取数据。我已经读过混合getline和这个很糟糕的做法,但我相信在这种情况下它会有意义。运行getline(),strcpy()和sscanf()都没有意义,当一个0(n)函数(>>)就足够时,它们都具有O(n)复杂度(这将测试速度) 。

我的问题是它在第1轮中正确地读取了明确的僵尸。然而,当它到达“---”意味着新的循环因此从while循环开始时开始,它完全退出两个循环。我已经研究过标志并且认为会发生故障,并且程序计数器将返回到初始while循环以在新一轮中读取。我尝试使用peek()没有成功,并且一条捷径肯定会让我失败一些测试用例。

我只是专注于修复最后的while循环,而不是重做下面列出的整个代码。如果需要更多信息,我很乐意添加更多信息。谢谢!

struct zombiesPerRound{
    unsigned int round;
    unsigned int numZombies;
};


struct zombie{
    unsigned int startDistance;
    unsigned int speed;
    unsigned int health;
    string name;
};

string line;
vector <zombiesPerRound> randomZombies;
list <zombie> masterList;
ifstream infile(inputFile);
if(infile.is_open()){
    //Read in the Player Information
    getline(infile,line);
    char *read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Quiver_Capacity: %u",&settings.numArrows);
    delete read;
    getline(infile,line);
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Random_Seed: %u",&settings.randomSeed);
    srand(settings.randomSeed);
    delete read;
    getline(infile,line);
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Max_Rand_Distance: %u",&settings.maxDistance);
    delete read;
    getline(infile,line);
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Max_Rand_Speed: %u",&settings.maxSpeed);
    delete read;
    getline(infile,line);
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Max_Rand_Health: %u",&settings.zombieHealth);
    delete read;
    getline(infile,line);
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Player_Health: %u",&settings.playerHealth);
    delete read;
    while(!infile.fail()){
        getline(infile,line);
        if(!line.substr(0,1).compare("-"))
            continue;
        zombiesPerRound randZombie;
        read=new char[line.length()+1];
        strcpy(read,line.c_str());
        sscanf(read,"Round: %u",&randZombie.round);
        delete read;
        getline(infile,line);
        read=new char[line.length()+1];
        strcpy(read,line.c_str());
        sscanf(read,"Num_Zombies: %u",&randZombie.numZombies);
        delete read;
        randomZombies.push_back(randZombie);
        zombie temp;
        //Read in the explicit zombies
        while(infile >> temp.startDistance >> temp.speed >> temp.health >> temp.name){
            masterList.push_back(temp);
        }
    }
}
infile.close();

//test1.txt
Quiver_Capacity: 10
Random_Seed: 2049231
Max_Rand_Distance: 50
Max_Rand_Speed: 60
Max_Rand_Health: 1
Player_Health: 10
---
Round: 1
Num_Zombies: 25
150 300 15 FoxMcCloud
2 3 6 FalcoLombardi
100 1 100 SlippyToad
---
Round: 3
Num_Zombies: 50
20 10 20 DarkLink

1 个答案:

答案 0 :(得分:0)

似乎以两种不同的方式阅读(getline和&gt;&gt;)对输入流做了一些不好的事情(说实话我不太明白)。

我已经解决了这个问题:

while(!infile.fail()){
    getline(infile,line);
    if(!line.substr(0,1).compare("-"))
        getline(infile,line);

    zombiesPerRound randZombie;
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Round: %u",&randZombie.round);
    delete read;
    getline(infile,line);
    read=new char[line.length()+1];
    strcpy(read,line.c_str());
    sscanf(read,"Num_Zombies: %u",&randZombie.numZombies);
    delete read;
    randomZombies.push_back(randZombie);
    zombie temp;
    //Read in the explicit zombies
    while(getline(infile,line)){
        if(!line.substr(0,1).compare("-"))
            break;
        stringstream  ss(line);
        ss >> temp.startDistance >> temp.speed >> temp.health >> temp.name;
        masterList.push_back(temp);
        infile.clear();
    }
}

} infile.close();

我很确定有更优雅/有效的方法,我用您发布的文件对其进行了测试,但它似乎可以正常工作。

希望有所帮助......