Hi, I am working on a project wherein one program writes to a file, but allows a different program to read from that file BEFORE the first program is finished writing to it. In this project, the first program is a game. When someone scores in the game, that activity is outputted to a file. A second program is running at that same time and displaying up to the minute scores from the game. Thus I'd like the first program to not grant itself exclusive rights to the output file. Here is my code.
// This program creates an output file that will be read by another
// program while this program is still writing to the output file.
#include "windows.h"
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream outs;
cout << "Going to sleep\n";
Sleep (5000);
cout << "Waking up: Opening log file\n";
outs.open("../DummyLogFile.txt", fstream::in | fstream::out);
if (outs.fail())
cout << "Unsuccessful opening log file for output\n";
else
cout << "Successful opening log file for output\n";
for (int i = 1; i <= 50; i++)
{
cout << "Line " << i << ": Hello world\n";
outs << "Line " << i << ": Hello world\n";
Sleep (1000);
}
outs.close ();
return 0;
}
The results of running this are:
Unsuccessful opening log file for output
When I remove the two fstream flags from the "open" statement, the ofstream is successfully opened in line 15. When I leave the flags in, it is not successful. Any ideas? Thank you.