Omar Ansari
Omar Ansari

Reputation: 21

ofStream printing in another function

I have a weird problem going on. I have two functions, which both have an ofstream passed by reference. However when I call the second function, part of the first function is being printed.

Here is the first function:

void GamePlay::dealDominos(ofstream& outStream, int seed){
    for(int i = 0; i < 10; ++i) 
    {

       outStream << "random " << rand() << endl; 

    }
}

My second function is:

void GamePlay::findLongestSeq(ofstream& outStream)
{
    outStream << toStringSeq(label, maxSeq) << endl;
}

However, my output looks like this:

NEW MAX [  T   0  8 ]
NEW MAX random [  T   0  8 ][  T   8  1 ]
NEW MAX ndom [  T   0  8 ][  T   8  1 ][  T   1  1 ][  T   1  2 ]
NEW MAX dom [  T   0  8 ][  T   8  1 ][  T   1  1 ][  T   1  2 ][  T   2 11 ]
MAX SEQ FOR:    dom [  T   0  8 ][  T   8  1 ][  T   1  1 ][  T   1  2 ][  T   2 11 ]

I don't want the word "random" to be printed between the label and the sequence..

How do I fix this?

Upvotes: 0

Views: 80

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171433

It looks like you've got two separate ofstream objects referring to the same underlying file, which is a pretty bad idea unless you're going to do lots of seek operations and flushes on every write, to ensure the file positions get updated for every write.

Upvotes: 1

Related Questions