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: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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.

  1. // This program creates an output file that will be read by another
  2. // program while this program is still writing to the output file.
  3.  
  4. #include "windows.h"
  5. #include <fstream>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. ofstream outs;
  12. cout << "Going to sleep\n";
  13. Sleep (5000);
  14. cout << "Waking up: Opening log file\n";
  15. outs.open("../DummyLogFile.txt", fstream::in | fstream::out);
  16. if (outs.fail())
  17. cout << "Unsuccessful opening log file for output\n";
  18. else
  19. cout << "Successful opening log file for output\n";
  20. for (int i = 1; i <= 50; i++)
  21. {
  22. cout << "Line " << i << ": Hello world\n";
  23. outs << "Line " << i << ": Hello world\n";
  24. Sleep (1000);
  25. }
  26. outs.close ();
  27. return 0;
  28. }

The results of running this are:

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

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
Last edited by tesuji; Apr 17th, 2008 at 8:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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?


  1. // This program creates an output file that will be read by another
  2. // program while this program is still writing to the output file.
  3.  
  4. #include "windows.h"
  5. #include <fstream>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. fstream outs;
  12. cout << "Going to sleep\n";
  13. Sleep (5000);
  14. cout << "Waking up: Opening log file\n";
  15.  
  16. // outs.open("DummyLogFile.txt", ios::in|ios::out);
  17. outs.open("DummyLogFile.txt", fstream::in|fstream::out);
  18. if (outs.fail())
  19. {
  20. cout << "Unsuccessful opening log file for output\n";
  21.  
  22. if (outs.bad ())
  23. {
  24. cout << "badbit is set\n";
  25. }
  26. }
  27. else
  28. cout << "Successful opening log file for output\n";
  29.  
  30. for (int i = 1; i <= 10; i++)
  31. {
  32. cout << "Line " << i << ": Hello world\n";
  33. outs << "Line " << i << ": Hello world\n";
  34. Sleep (1000);
  35. }
  36. outs.close ();
  37. return 0;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

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
Last edited by vijayan121; Apr 17th, 2008 at 9:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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?
Last edited by VernonDozier; Apr 17th, 2008 at 10:34 pm. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Trying to write to a file so that another program can read from it at the same ti

 
1
  #7
Apr 18th, 2008
Originally Posted by VernonDozier View Post
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?


  1. // This program creates an output file that will be read by another
  2. // program while this program is still writing to the output file.
  3.  
  4. #include "windows.h"
  5. #include <fstream>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. fstream outs;
  12. cout << "Going to sleep\n";
  13. Sleep (5000);
  14. cout << "Waking up: Opening log file\n";
  15.  
  16. // outs.open("DummyLogFile.txt", ios::in|ios::out);
  17. outs.open("DummyLogFile.txt", fstream::in|fstream::out);
  18. if (outs.fail())
  19. {
  20. cout << "Unsuccessful opening log file for output\n";
  21.  
  22. if (outs.bad ())
  23. {
  24. cout << "badbit is set\n";
  25. }
  26. }
  27. else
  28. cout << "Successful opening log file for output\n";
  29.  
  30. for (int i = 1; i <= 10; i++)
  31. {
  32. cout << "Line " << i << ": Hello world\n";
  33. outs << "Line " << i << ": Hello world\n";
  34. Sleep (1000);
  35. }
  36. outs.close ();
  37. return 0;
  38. }
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.
Last edited by Agni; Apr 18th, 2008 at 2:08 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

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:
  1. ios_base Flags Meaning
  2. in Reads (file must exist)
  3. out Empties and writes (creates if necessary)
  4. out | trunc Empties and writes (creates if necessary)
  5. out | app Appends (creates if necessary)
  6. in I out Reads and writes; initial position is the start (file must exist)
  7. 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').
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Trying to write to a file so that another program can read from it at the same ti

 
0
  #10
Apr 18th, 2008
Originally Posted by littlestone View Post
The various combinations of the flags and the corresponding meaning as below:
  1. ios_base Flags Meaning
  2. in Reads (file must exist)
  3. out Empties and writes (creates if necessary)
  4. out | trunc Empties and writes (creates if necessary)
  5. out | app Appends (creates if necessary)
  6. in I out Reads and writes; initial position is the start (file must exist)
  7. 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.
Last edited by VernonDozier; Apr 18th, 2008 at 6:33 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC