Reading a file avoids writing to it
-
Hi all!
I'm facing a file handling problem that I don't understand (yet :-)There is one application writing a short line into a text file, every minute. Its source code is not available.
My application opens this QFile QIODevice::ReadOnly | QIODevice::Text and reads from it every 10 seconds. The file leaves opened all time.Now following happens: The first application stops writing, until my app. is aborted manually.
That means: Reading the file avoids writing to it.
Why? Are there any hints? -
Welcome to devnet
The file is opened exclusively for writing and simply is not opened when opened by another process.
Alternatively, if there is always only this one line in the file, I would assume that the file is freshly opened each time for writing. It should be similar to open a file without closing it. In your case the content will be written to the new file, which you have NOT opened. Therefore, you cannot find the new information.
The only chances I see is that you are opening repeatably as well. However, you have to ensure that the file is not opened while the other application is writing.
-
Hi koahnig,
thanks for your post.I already tried to open with ReadWrite flags, to allow the 1st application to go on writing. But it doesn't change anything.
Indeed I'm thinking about "open on demand":
-Wait till file date changed- open file
- read
- close
That should work, but pretty looks like a workaround...
-
[quote author="Asohen" date="1422182419"]
I already tried to open with ReadWrite flags, to allow the 1st application to go on writing. But it doesn't change anything.
[/quote]
ReadWrite flag is very likely worse than ReadOnly. First of all it describes the handling where you are opening. Since your new applciation does not require writing permission, ReadOnly shall be sufficient.
Furthermore, a ReadWrite flag shall reserve the file for your application and the other application has no chance to open it. -
Why do you read the file every ten seconds, instead of only after it has changed?
If you use a QFileSystemWatcher to monitor the file, you can do your reading when there is actually something new to read. That will also lower the risk of you blocking the write with your reading.
Other than that: do your reading as fast as you can, and close the file again. Only after your reading, you start processing its contents. Fastest is probably to use QFile::readAll to read the contents of your file to a QByteArray. Then you can use a QBuffer on your QByteArray to access it as if it is the file itself.
-
Indeed, I have to change reading. The file will be closed until the writing application appends data to it. Then I will open it again, read and close, and so on.
No problem so far.But I started this thread to understand, why the writing thread stops writing as soon as my reading application reads. This point is not clear yet.
-
-
[quote author="cdietz" date="1422367650"]If you are on windows, your application blocks the file's handle.[/quote]
Thanks for the hint. "Of course" I'm on Windows :-|OK, it's a characteristic of windows - not my clumsy programming.
Changing to a open-read-close logic. -