c++ - 错误地读取文件

标签 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
}

等等。 有人可以解释为什么 2 和 3 最初被设置为 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";
}

block 写入:

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

文本 block 写入:

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";
}

最佳答案

我希望它能将文件读取为:

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

因为它总是一次读取三个字符串。如果您希望始终读取三个字符串,您可能希望使用虚拟 0 来填充 MB 和 BB 指标以进行读取(例如 MB 0 0)。

这可能有助于认识到这一点

cin >> a >> b >> c;cin >> a; 没有什么不同cin >> b; cin >> c; 当涉及到处理换行符时。

关于c++ - 错误地读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012918/

相关文章:

c++ - 如何配置 Qt Creator 以适用于普通的 C++ 项目?

c++ - 使用 zlib 解压缩 .zip 文件的简单方法

c++ - 为什么当客户端忙于接收数据时 select() 有时会超时

C++ 创建命名管道 ERROR_PATH_NOT_FOUND (3)

C 文件读取 getchar 在 32KB 后产生随机 EOF

android - 将 Arraylist 保存到文件 android

loops - 循环文件时数组元素被删除

c++ - 如何在没有第三方软件的情况下轮询鼠标宏键?

c - 如何减少C中文件指针的值?

c - 修改c中文件的现有内容