Strange issue with cout

Posted by ben on Stack Overflow See other posts from Stack Overflow or by ben
Published on 2014-08-18T15:35:18Z Indexed on 2014/08/18 16:22 UTC
Read the original article Hit count: 109

Filed under:

After reading from a text file, and storing it into a string, I tried to pass this message into another function that will print it out. This is what it looks like:

http://imgur.com/MCjfRdp

void insert (int id, string message){
    cout << "Inserting " << message << " at" << endl;

}

Somehow the message behind the string overrides the message. But once I removed the " at" after message, it worked as expected. http://imgur.com/JdHPPmi

 void insert (int id, string message){
    cout << "Inserting " << message << endl;

}

I somehow suspect the problem came from stringstream, but i couldn't find out where. Here's the code that read from file

    vector <string> line;
fstream infile;
string readLine, tempLine, action, tempLine1;
int level, no;
infile.open(fileName.c_str(),ios::in);
int i = 0;

while (!infile.eof())
{
    getline (infile, readLine);
    line.push_back(readLine);
}
infile.close();
line.pop_back();
for (int i = 0; i < line.size();i++)
{
    stringstream ss (line.at(i));
    getline (ss, action, ' ');

    if (action == "init")
    {
        // some other work here
    }
    else if (action == "insert")
    {
        tempLine = line.at(i);
        ss >> no;

        stringstream iline(tempLine);
        getline (iline, tempLine1, ' ');
        getline (iline, tempLine1, ' ');
        getline (iline, tempLine1, '\n');
        Insert (no, tempLine1);

    }

Since my file has different kinds of actions, here is what test.dat contains:

insert 0 THIS IS A TEST

edit: When i did > file.txt, it came out as something like this.

Inserting THIS IS A TEST at Inserting THIS IS TEST NO 2 at

© Stack Overflow or respective owner

Related posts about c++