943,829 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1918
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 17th, 2008
0

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

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,374 posts
since Jan 2008
Apr 17th, 2008
1

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

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.
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Apr 17th, 2008
0

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

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)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,374 posts
since Jan 2008
Apr 17th, 2008
1

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

> 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 17th, 2008
0

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

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
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,374 posts
since Jan 2008
Apr 18th, 2008
1

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

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).
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 18th, 2008
1

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

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)
  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

Quote ...
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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Apr 18th, 2008
0

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

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,374 posts
since Jan 2008
Apr 18th, 2008
1

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

The various combinations of the flags and the corresponding meaning as below:
C++ Syntax (Toggle Plain Text)
  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').
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Apr 18th, 2008
0

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

The various combinations of the flags and the corresponding meaning as below:
C++ Syntax (Toggle Plain Text)
  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:

Quote ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,374 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reading the serial port
Next Thread in C++ Forum Timeline: Trying to Build MySQL++ Libraries in Windows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC