user1814893
user1814893

Reputation:

incorrectly reading in file

If I give my program the txt file:

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

It reads it in incorrectly and instead reads it as

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

Here is a simplified version of what I am using to read this:

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
}

and so on. Can someone explain why two and three are initially being set to 0?

Full version of code:

To read:

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

To write:

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 write:

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

TextBlock write:

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

Enemy write:

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

Upvotes: 2

Views: 198

Answers (3)

user2448027
user2448027

Reputation: 1638

The line

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

reads three strings from a file instead of one.

Upvotes: 0

Christian Ammer
Christian Ammer

Reputation: 7542

The reason for the number triples that you got:

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

is following: Your input only gets read from the first loop:

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

Like following:

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

The last three columns in the table are the observed number triples.

Upvotes: 1

Mark B
Mark B

Reputation: 96311

I would have expected it to read the file as:

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

because it's always reading three strings at a go. If you want to always read three strings you may wish to pad your MB and BB indicators with dummy 0s to read (For example MB 0 0).

It may help to realize that

cin >> a >> b >> c; is no different from cin >> a; cin >> b; cin >> c; when it comes to handling newlines.

Upvotes: 2

Related Questions