简体   繁体   English

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

[英]Extracting Data in different formats in a Text File

I'm having an issue with reading in data and I've been researching it for the past hour to no better conclusion. 我在读取数据时遇到问题,并且过去一个小时一直在研究它,但没有更好的结论。 Pasted below are snippets of my code and the file I'm reading form. 下面粘贴的是我的代码段和我正在读取的文件格式。 The file is opened properly and the struct declarations are also shown below as well. 该文件已正确打开,并且结构声明也在下面显示。 I'm confident the solution is simple and that I'm overthinking due to working on this project too long but here's my explanation. 我有信心解决方案很简单,并且由于在这个项目上工作了太长时间,所以我想得太多,但这是我的解释。

The data file gets opened properly and all the data gets read into the appropiate place until the while(infile>>temp.startDistance.....) part. 数据文件被正确打开,所有数据都被读取到适当的位置,直到while(infile >> temp.startDistance .....)部分为止。 My goal at this point is to read in the zombies stated explicitly in each round after the number of random zombies is read( see data file for format). 我现在的目标是在读取随机僵尸数量后,在每一轮明确读取的僵尸中读取(有关格式,请参阅数据文件)。 In the previous cases of reading in data, i used getline, than sscanf on the c string version of the string received from getline to extract the appropiate data. 在以前的读取数据的情况下,我使用getline而不是从getline接收的字符串的c字符串版本上的sscanf提取适当的数据。 Since the explicit zombies are formatted simply with just white space separating the data, I wanted to use the >> operater to extract the data. 由于显式僵尸仅用空格分隔数据即可格式化,因此我想使用>>运算符提取数据。 I've read it's poor practice to mix getline and this but I believe in this case it would make sense. 我读过混合getline和这是一个不好的做法,但是我相信在这种情况下这是有道理的。 There would be no point in running getline(),strcpy(), and sscanf() which all have O(n) complexity when one 0(n) function (>>) would suffice (This will be tested for speed). 当一个0(n)函数(>>)足够时,运行getline(),strcpy()和sscanf()都具有O(n)复杂度是没有意义的(将测试速度)。

My issue is it reads in the explicit zombies properly in round 1. However, when it reaches the "---" meaning new round so start at beginning of while loop, it exits out of both loops entirely. 我的问题是,它在第​​1轮中正确地读取了明确的僵尸。但是,当到达“ --​​-”意味着新的一轮时,因此从while循环的开始开始,它将完全退出两个循环。 I've researched flags and thought a failbit would occur and the program counter would go back to the initial while loop to read in a new round. 我研究了标志并认为会发生故障位,并且程序计数器会返回到初始的while循环以进行新的循环读取。 I've tried using peek() to no success and a shortcut that would certainly make me fail some test cases potentially. 我尝试过使用peek()失败,而使用快捷方式肯定会使我无法通过某些测试用例。

I'm just focusing on fixing the final while loop rather than redoing the entire code listed below. 我只是专注于修复最终的while循环,而不是重做下面列出的整个代码。 If any more information is needed, I'll be glad to add more. 如果需要更多信息,我会很乐意添加更多信息。 Thanks ! 谢谢 !

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

It seems that reading in two different ways (the getline and the >>) does something bad to the input stream (something i don't quite understand to be honest). 看来,以两种不同的方式阅读(getline和>>)会对输入流造成不利影响(说实话,我不太理解)。

I've fixed the problem with this: 我已经解决了这个问题:

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(); } infile.close();

I'm pretty sure that there are more elegant / efficient ways to do it, I tested this with the file you posted and it seems to work though. 我敢肯定,有更优雅/有效的方法可以做到这一点,我用您发布的文件对此进行了测试,但它似乎可以正常工作。

Hope it helps.. 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM