BAdhi
BAdhi

Reputation: 510

Writing Integers to stream

I have created a library which has a portion of code similar to following

int a;
a = 5;

std::ofstream outFile("File.txt");
outFile << "Values : " << std::endl;
outFile << a << std::endl;

Now, this library is used by two different process which outputs File.txt with two different outputs

Output 1 :

Values :

Ouptut 2 :

Values :
5

I've found some resources where its mentioned to pass integers to streams by converting to strings using std::to_string (Converting integer to string in c++) . But I want to know what makes the two processes act differently in the same scenario. It feels like as if one of the process do some changes to the streams in a global state

Upvotes: 0

Views: 636

Answers (1)

Dawson Moore
Dawson Moore

Reputation: 50

Try running the two different processes in different directories. From what you've shown, more than likely, they both are trying to access the same file. This isn't something you can do. Check out this post which talks about why trying to use threads to open the same file twice at the same time doesn't work.

Upvotes: 1

Related Questions