| | |
Trying to write to a file so that another program can read from it at the same time
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
Trying to write to a file so that another program can read from it at the same time
0
#1 Apr 17th, 2008
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.
The results of running this are:
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.
C++ Syntax (Toggle Plain Text)
// 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:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Re: Trying to write to a file so that another program can read from it at the same time
1
#2 Apr 17th, 2008
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
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
Last edited by tesuji; Apr 17th, 2008 at 8:27 pm.
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
Re: Trying to write to a file so that another program can read from it at the same ti
0
#3 Apr 17th, 2008
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?
C++ Syntax (Toggle Plain Text)
// 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; }
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
Re: Trying to write to a file so that another program can read from it at the same ti
1
#4 Apr 17th, 2008
> 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
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
Last edited by vijayan121; Apr 17th, 2008 at 9:39 pm.
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
Re: Trying to write to a file so that another program can read from it at the same ti
0
#5 Apr 17th, 2008
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?
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?
Last edited by VernonDozier; Apr 17th, 2008 at 10:34 pm. Reason: typo
Re: Trying to write to a file so that another program can read from it at the same ti
1
#6 Apr 18th, 2008
You could use the WinAPI file functions (http://msdn2.microsoft.com/en-us/lib...32(VS.85).aspx) which allow you to set sharing permissions (as well as other flags and such for the sort of thing you described).
Re: Trying to write to a file so that another program can read from it at the same ti
1
#7 Apr 18th, 2008
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
// 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; }
•
•
•
•
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
Last edited by Agni; Apr 18th, 2008 at 2:08 am.
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
Re: Trying to write to a file so that another program can read from it at the same ti
0
#8 Apr 18th, 2008
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.
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
Re: Trying to write to a file so that another program can read from it at the same ti
1
#9 Apr 18th, 2008
The various combinations of the flags and the corresponding meaning as below:
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').
C++ Syntax (Toggle Plain Text)
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)
for you purpose, I think use flag 'out' will be ok. or use ofstream without flag(the default flag for ofstream is 'out').
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
Re: Trying to write to a file so that another program can read from it at the same ti
0
#10 Apr 18th, 2008
•
•
•
•
The various combinations of the flags and the corresponding meaning as below:
In your program, you use fifth combination, so the file you try to open must exist.C++ Syntax (Toggle Plain Text)
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)
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.
Edit: Just noticed death_oclock's links, which I missed before. Thank you for those.
Last edited by VernonDozier; Apr 18th, 2008 at 6:33 pm.
![]() |
Similar Threads
- Help with linked list in Turbo Pascal! (Pascal and Delphi)
- read text file (C)
- Read/write to same file > once, Help (C++)
- file open code in VB (Visual Basic 4 / 5 / 6)
- Need direction on how start this program (C++)
- needed big time hw due and late (C)
- how to find out the time of cpu? (Motherboards, CPUs and RAM)
- C++ Error : opening file a second time for a read (C++)
Other Threads in the C++ Forum
- Previous Thread: reading the serial port
- Next Thread: Trying to Build MySQL++ Libraries in Windows
| Thread Tools | Search this Thread |
api application array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






