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.

Recommended Answers

All 11 Replies

ofstream is for output, which can be combined with fstream::out | fstream::app // for append
but not with fstream::in | fstream::out

if you want to do both, use fstream.
you may have a look at http://www.daniweb.com/forums/thread6542.html

krs, cliff

Thanks for the response and the link. As far as I can tell, I'm doing it right, but I still get the same result: the error message and no output file. Here's my updated code. As you can see from lines 16 and 17, I tried using both "ios" and "fstream" in front of "in" and "out". Neither worked. I didn't get the message "badbit is set" in line 24, so that means that it must be the failbit, right?

// 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 ()
{
	fstream outs;
	cout << "Going to sleep\n";
	Sleep (5000);
	cout << "Waking up: Opening log file\n";
	
//	outs.open("DummyLogFile.txt", ios::in|ios::out);
	outs.open("DummyLogFile.txt", fstream::in|fstream::out);	
	if (outs.fail())
	{
		cout << "Unsuccessful opening log file for output\n";
		
		if (outs.bad ())
		{
                      cout << "badbit is set\n";
                }
         }
	else
		cout << "Successful opening log file for output\n";

	for (int i = 1; i <= 10; i++)
	{
		cout << "Line " << i << ": Hello world\n";
		outs << "Line " << i << ": Hello world\n";
		Sleep (1000);
	}
	outs.close ();
	return 0;
}

> a different program to read from that file BEFORE the first program is finished writing to it.
the output to a file is buffered at many levels: by the fstreambuf of the c++ library and by the operating system (cache manager). to achieve coherence between the two processes' view of the file, consider memory mapping the file.
see: http://msdn2.microsoft.com/en-us/library/aa914748.aspx
and http://msdn2.microsoft.com/en-us/library/aa914405.aspx

Thanks for the links. I will research them. I figured that I would somehow have to coordinate between the two programs and that there would be more involved than just the right fstream flags since I am using two programs instead of just one. Looks like what you linked involves "Handles", which is a brand new concept for me, so I have some reading to do. Again, thanks for the link and I will pursue it.

That aside, the particular example in this thread doesn't even try to read anything. It's just trying to open the file so it can be read, and there's only one program here, not two. I was going to try to bite this off a little at a time, but I'm hitting a hurdle earlier than I expected. Can anyone spot a problem with the code I listed above. Shouldn't the file at least open correctly?

Thanks for the response and the link. As far as I can tell, I'm doing it right, but I still get the same result: the error message and no output file. Here's my updated code. As you can see from lines 16 and 17, I tried using both "ios" and "fstream" in front of "in" and "out". Neither worked. I didn't get the message "badbit is set" in line 24, so that means that it must be the failbit, right?

// 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 ()
{
	fstream outs;
	cout << "Going to sleep\n";
	Sleep (5000);
	cout << "Waking up: Opening log file\n";
	
//	outs.open("DummyLogFile.txt", ios::in|ios::out);
	outs.open("DummyLogFile.txt", fstream::in|fstream::out);	
	if (outs.fail())
	{
		cout << "Unsuccessful opening log file for output\n";
		
		if (outs.bad ())
		{
                      cout << "badbit is set\n";
                }
         }
	else
		cout << "Successful opening log file for output\n";

	for (int i = 1; i <= 10; i++)
	{
		cout << "Line " << i << ": Hello world\n";
		outs << "Line " << i << ": Hello world\n";
		Sleep (1000);
	}
	outs.close ();
	return 0;
}

i dont see anything wrong with the code above and when i executed this i got this output

Going to sleep
Waking up: Opening log file
Successful opening log file for output
Line 1: Hello world
Line 2: Hello world
Line 3: Hello world
Line 4: Hello world
Line 5: Hello world
Line 6: Hello world
Line 7: Hello world
Line 8: Hello world
Line 9: Hello world
Line 10: Hello world

edit: forgot to mention that the output is succesfully written to the file also. I hope you have the right file in the right folder.

commented: Thank you for taking the time to run my code. +2

It worked for you, huh? I thought it SHOULD work, but I just tried it again on my compiler (Dev C++), and it failed again. Did the same earlier with Visual C++ 2008. Just to make extra sure , I just copied and pasted it directly from the post you just wrote to be sure we ran the exact same thing. Totally different results. Bizarre. Thanks for trying it out. Guess I'll try it another computer. Can't imagine why that would make a difference though.

The various combinations of the flags and the corresponding meaning as below:

ios_base Flags 	         Meaning 
in 	                        Reads (file must exist)
out 	                       Empties and writes (creates if necessary)
out | trunc 	            Empties and writes (creates if necessary)
out | app 	            Appends (creates if necessary)
in I out 	              Reads and writes; initial position is the start (file must exist)
in | out | trunc 	   Empties, reads, and writes (creates if necessary)

In your program, you use fifth combination, so the file you try to open must exist.

for you purpose, I think use flag 'out' will be ok. or use ofstream without flag(the default flag for ofstream is 'out').

commented: Helpful +2

The various combinations of the flags and the corresponding meaning as below:

ios_base Flags 	         Meaning 
in 	                        Reads (file must exist)
out 	                       Empties and writes (creates if necessary)
out | trunc 	            Empties and writes (creates if necessary)
out | app 	            Appends (creates if necessary)
in I out 	              Reads and writes; initial position is the start (file must exist)
in | out | trunc 	   Empties, reads, and writes (creates if necessary)

In your program, you use fifth combination, so the file you try to open must exist.

for you purpose, I think use flag 'out' will be ok. or use ofstream without flag(the default flag for ofstream is 'out').

Ah, thank you, I hadn't realized that the flag requires that the file exists. When I place a file there, it DOES open successfully. I kind of figured it wouldn't need to test the file for existence as it does with an ifstream since I could create it and write to it with the "out" option, THEN read from it with the "in" option, but apparently that isn't allowed or it at least isn't allowed the way I tried it. I'm going to research vijayan121's links to see if I can get them to work for me. Chandra.rajat, out of curiosity, did you create an empty file first before you ran it successfully? I reread your post and am now interpreting this line:

I hope you have the right file in the right folder.

as you creating a file ahead of time in order for it to work. Thanks everyone!

Edit: Just noticed death_oclock's links, which I missed before. Thank you for those.

> I'm going to research vijayan121's links to see if I can get them to work for me.
if you just need to communicate between the two processes, a file is not required at all. you could use a standard IPC technique (shared memory, pipes, sockets, rpc). a file is required only if you want the data to persist after process termination.

> I'm going to research vijayan121's links to see if I can get them to work for me.
if you just need to communicate between the two processes, a file is not required at all. you could use a standard IPC technique (shared memory, pipes, sockets, rpc). a file is required only if you want the data to persist after process termination.

My particular application is to parse log files of online games like Unreal Tournament, Call Of Duty, and any game that produces a text log file, and then to use a second application to display these text files graphically. The assumption is that the two applications would be written by two development teams that would have no coordination with each other. Most games don't allow access to the log files until the game is over, but I was researching a way that a game could be designed that allowed other programs to access its log file WHILE the log file was still being written to occasionally. I'm basically creating my own little game that produces a log file with data and having a completely separate program parse it simultaneously, the assumption again being that this second program would not have any access to the first program except possibly that the first allows real-time access to its outputted text log files.

Your "socket" idea is an interesting one too though and I think I'll also pursue writing the game so that it provides a socket option for that second program. But I'm going to mark this one as solved since my original roadblock with the fstream flags has been solved. Thanks for all the help everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.