错误地读取文件

时间:2013-06-09 18:42:31

标签: c++ file-handling

如果我给我的程序提供txt文件:

BB
MB
150 570 2 
240 570 3 
360 570 0 
FB
E
T

它读取错误,而是将其读作

BB
150 0 0 
240 570 2 
360 570 3 
0 570 0 
MB
FB
E
T

以下是我用来阅读此内容的简化版本:

string one,two,three,four;

ifstream file;
filename+=".txt";//filename is a string
file.open(filename.c_str());

while (file >> one >> two>>three&&one!="MB")
{
//do stuff with it here
}

等等。 有人可以解释为什么两个和三个最初被设置为0?

完整版代码:

阅读:

void load(string filename)
{
    string one,two,three,four;

    ifstream file;
    filename+=".txt";
    file.open(filename.c_str());
    //blocks

    //backblock list
    while (file >> one >> two>>three&&one!="MB")
    {
        backBlockList.push_back(
            Block(atoi(two.c_str()),atoi(three.c_str()),atoi(one.c_str())));
    }
    while (file >> one >> two>>three&&one!="FB")
    {
        midBlockList.push_back(
            Block(atoi(two.c_str()),atoi(three.c_str()),atoi(one.c_str())));
    }
    while (file >> one >> two>>three&&one!="E")
    {
        foreBlockList.push_back(
            Block(atoi(two.c_str()),atoi(three.c_str()),atoi(one.c_str())));
    }
    while (file >> one &&one!="T")
    {
        enemyList.push_back(Enemy(atoi(one.c_str())));
        //loads waypoints
        while (file >> one>>two )
        {
            enemyList.at(enemyList.size()-1).addWaypoint(
                atoi(one.c_str()),atoi(two.c_str()));
        }
        while(file>>one>>two>>three>>four)
        {
            textBlockList.push_back(
                TextBlock(atoi(one.c_str()),atoi(two.c_str())));
            textBlockList.at(
                textBlockList.size()-1).setText(three);
            textBlockList.at(
                textBlockList.size()-1).setRange(atoi(four.c_str()));
        }
    }
}

写:

void printOut(string filename )
{
    cout<<"printing "<<endl;
    ofstream myfile;
    filename+=".txt";
    myfile.open (filename.c_str());
    myfile << "BB\n";

//prints out blocks
    cout<<"printing backblocks";
    unsigned int i = 0;
    for(  i = 0; i<backBlockList.size(); i++)
    {
        backBlockList.at(i).print(myfile);
    }
    cout<<" printed "<<i<<endl;
    cout<<"printing midblocks";
    myfile << "MB\n";
    for(  i = 0; i<midBlockList.size(); i++)
    {
        midBlockList.at(i).print(myfile);
    }
    cout<<" printed "<<i<<endl;
    cout<<"printing foreblocks";
    myfile << "FB\n";
    for(  i = 0; i<foreBlockList.size(); i++)
    {
        foreBlockList.at(i).print(myfile);
    }
    cout<<" printed "<<i<<endl;
    cout<<"printing enemies "<<endl;
    myfile<<"E\n";
    for(  i =0; i<enemyList.size(); i++)
    {
        enemyList.at(i).print(myfile);
    }
    cout<<"printing text";
    myfile<<"T\n";
    for(  i =0; i<textBlockList.size(); i++)
    {
        if(textBlockList.at(i).complete())
            textBlockList.at(i).print(myfile);
    }
    cout<<" printed "<<i<<endl;
    cout<<"printing other"<<endl;
//Additional stuff goes here EX BACKGROUND

    myfile.close();
    cout<<"printing done";
}

阻止写:

void Block::print(ofstream & file)
{
    file << x;
    file << " ";
    file<< y;
    file<< " ";
    file<< Type;
    file<< " \n";
}

TextBlock写道:

void TextBlock::print(ofstream & file)
{
    file<< x;
    file<<" ";
    file<< y;
    file<<" ";
    file<< text;
    file<<" ";
    file<<range;
    file<<" \n";
}

敌人写道:

void Enemy::print(ofstream & file)
{
    file<<type;
    for(unsigned int i =0; i<X.size()-1; i++)
    {
        file<<" ";
        file<<   X.at(i);
        file<<" ";
        file<<   Y.at(i);
    }

    file<<"\n";
}

3 个答案:

答案 0 :(得分:2)

我原以为它会把文件读成:

BB MB 150
570 2 240
570 3 360
570 0 FB
E T

因为它总是一直读三个字符串。如果您想要总是读取三个字符串,您可能希望用虚拟0 s来填充MB和BB指示符(例如MB 0 0)。

可能有助于实现

在处理换行符时,

cin >> a >> b >> c;cin >> a; cin >> b; cin >> c;没什么区别。

答案 1 :(得分:1)

您获得的数字三倍的原因:

150 0 0 
240 570 2 
360 570 3 
0 570 0 

如下:您的输入仅从第一个循环中读取:

while (file >> one >> two>>three&&one!="MB")

如下:

Loop | one | two | three | atoi | atoi | atoi
     |     |     |       | one  | two  | three
----------------------------------------------
1    |  BB |  MB |   150 |    0 |    0 |   150
2    | 570 |   2 |   240 |  570 |    2 |   240
3    | 570 |   3 |   360 |  570 |    3 |   360
4    | 570 |   0 |    FB |  570 |    0 |     0
5    | breaks the loop because three can't be read

表格中的最后三列是观察到的三元组数。

答案 2 :(得分:0)

该行

file >> one >> two>>three&&one!="FB"

从文件中读取三个字符串而不是一个。