Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 提取文本文件中不同格式的数据_C++_Fstream_Getline - Fatal编程技术网

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

C++ 提取文本文件中不同格式的数据,c++,fstream,getline,C++,Fstream,Getline,我在阅读数据方面遇到了一个问题,在过去的一个小时里我一直在研究这个问题,没有得出更好的结论。下面粘贴的是我的代码片段和我正在阅读的文件表单。文件已正确打开,结构声明也显示在下面。我相信这个解决方案很简单,而且由于在这个项目上工作的时间太长,我想得太多了,但下面是我的解释 数据文件被正确打开,所有数据被读取到适当的位置,直到while(infle>>temp.startDistance…)部分。我在这一点上的目标是在读取随机僵尸的数量后,读入每一轮中明确说明的僵尸(格式见数据文件)。在前面读取数据

我在阅读数据方面遇到了一个问题,在过去的一个小时里我一直在研究这个问题,没有得出更好的结论。下面粘贴的是我的代码片段和我正在阅读的文件表单。文件已正确打开,结构声明也显示在下面。我相信这个解决方案很简单,而且由于在这个项目上工作的时间太长,我想得太多了,但下面是我的解释

数据文件被正确打开,所有数据被读取到适当的位置,直到while(infle>>temp.startDistance…)部分。我在这一点上的目标是在读取随机僵尸的数量后,读入每一轮中明确说明的僵尸(格式见数据文件)。在前面读取数据的案例中,我使用getline,而不是从getline接收的字符串的c字符串版本上的sscanf来提取适当的数据。由于显式zombie的格式只是用空格分隔数据,因此我想使用>>操作符来提取数据。我读过混合使用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
struct zombiesPerRound{
无符号整数舍入;
未签名整数numZombies;
};
结构僵尸{
无符号整数起始位置;
无符号整数速度;
未签名整数健康;
字符串名;
};
弦线;
矢量随机僵尸;
列表主列表;
ifstream infle(输入文件);
if(infle.is_open()){
//读入播放器信息
getline(填充,行);
char*read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读为“箭袋容量:%u”&settings.numArrows);
删除读取;
getline(填充,行);
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读取“随机种子:%u”、&settings.randomSeed);
srand(settings.randomSeed);
删除读取;
getline(填充,行);
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读取“最大随机距离:%u”、&settings.maxDistance);
删除读取;
getline(填充,行);
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读取“最大速度:%u”、&settings.maxSpeed);
删除读取;
getline(填充,行);
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读为“最大运行状况:%u”,&settings.zombieHealth);
删除读取;
getline(填充,行);
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读取“玩家健康:%u”和设置.playerHealth);
删除读取;
而(!infle.fail()){
getline(填充,行);
如果(!line.substr(0,1).比较(“-”)
继续;
僵尸倒下了,僵尸;
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读为“轮数:%u”&randZombie.Round);
删除读取;
getline(填充,行);
read=新字符[line.length()+1];
strcpy(read,line.c_str());
sscanf(读为“Num_僵尸:%u”、&randZombie.numZombies);
删除读取;
随机僵尸。推回(随机僵尸);
僵尸温度;
//读入显式僵尸
同时(填充>>温度起点>>温度速度>>温度健康>>温度名称){
主列表。推回(临时);
}
}
}
infle.close();
//test1.txt
箭袋容量:10
随机种子:2049231
最大距离:50
最高速度:60
最大健康指数:1
玩家健康:10
---
第二轮:1
僵尸数量:25
150 300 15福克斯云
2 3 6 FalcoLombardi
100 1 100拖鞋头
---
第二轮:3
僵尸数量:50
20 10 20黑暗

以两种不同的方式(getline和>>)阅读似乎会对输入流造成不良影响(老实说,我不太理解这一点)

我已经解决了这个问题:

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

我很确定有更优雅/高效的方法可以做到这一点,我用你发布的文件对此进行了测试,但似乎效果不错

希望能有帮助