1

I am using VC++ to generate a text file and write (via fstream) data continuously to it. I have another application2 (NOT C++) which accesses that same file which c++ appends. At the instant application2 accesses the file, new data from C++ program cannot be written to it. It seems like the new data from c++ goes to some temporary file. when application2 closes the file, new data gets updated to the file. I want the data to be written to the file in real time and be read at the same time by application2. What should I do in c++ to make new data appear in the file which is opened by application2?

C++ side:

int realTimeValues  // this variable is updated continuously

FILE * pFileTXT; 

while(1)
{
 pFileTXT = fopen ("realTimeData.txt","a"); // Opening file in append mode
 fprintf (pFileTXT, "%d\n",realTimeValues);   // saving values to file
 fclose (pFileTXT)                            // Closing the file
}

On the application2 side I can't tell how exactly its opening this file. The application is Universal Real-Time Software Oscilloscope. In the menu there is an option "read from a file"

Osaid
  • 557
  • 1
  • 8
  • 23
  • 1
    Could you use Unix pipes or sockets instead of direct streams? The OS can happily handle the events and buffers for you. – Phil H Nov 02 '12 at 16:42
  • You can't have a reader and a writer accessing a file at the same time if that's what your asking for. The OS and not c++ is responsible for controlling access to that file. If you have to use a file your stuck with this limitation. – andre Nov 02 '12 at 16:54
  • I thought that too, but now I think I was wrong (I deleted my answer). Reading from the file should be possible by default. I think the problem in this case is the buffering. You need to flush the stream to force an immediate update of the file. – Nikos C. Nov 02 '12 at 16:58
  • the scenario I am trying to implement is more of a ring buffer, instead of buffer its a file. application1 updates it and application2 reads it. (OS Windows 7) – Osaid Nov 02 '12 at 17:19
  • 1
    @ahenderson, really? how `tail -f` works then? –  Nov 02 '12 at 17:21
  • 1
    @ahenderson That's obviously wrong. There are plenty of tailing applications the read logs c++ (and any other language for that matter) applications are actively writing to. As I said in my answer, the problem is with how application2 opens the file. It has nothing to do with the application writing the file. – evanmcdonnal Nov 02 '12 at 17:27
  • You can know how the file is open by the other application using Process monitor. – pagra Dec 12 '12 at 10:40

1 Answers1

0

Ugh. There are a number of things at work:

  1. By default, fprintf output may be buffered in a private ram buffer. This buffer is flushed only as needed or when you do fclose. If you want to force the data out, there's a setbuf call you can make (read the docs) or explicitly call fflush after each fprintf.
  2. I do not know if fopen allows for simultaneous readers. Probably not, given the buffering behavior. If you want read/write sharing, you should look at the documentation to see if there is an explicit mode parameter to enable this.
  3. Unfortunately, this only covers the part you can control. If the reader (an application you do not control) doesn't support shared reading, then you're out of luck.
MJZ
  • 1,074
  • 6
  • 12
  • 1. if the program continuously writes then the buffer will keep filling up and getting flushed, nothing extra needed. 2. multiple readers is entirely up to the OS. Although Windows stupidly defaults to locking files in order to maintain DOS compatibility it's still possible to not lock files. – bames53 Dec 12 '12 at 06:03