1

I'm writing a program that performs several tests on a hardware unit, and logs both the results of each test and the steps taken to perform the test. The trick is that I want the program to log these results to a text file as they become available, so that if the program crashes the results that had been obtained are not lost, and the log can help debug the crash.

For example, assume a program consisting of two tests. If the program has finished the first test and is working on the second, the log file would look like:

Results:
Test 1 Result A: Passed
Test 1 Result B: 1.5 Volts

Log:
Setting up instruments.
Beginning test 1.
[Steps in test 1]
Finished test 1.
Beginning test 2.
[whatever test 2 steps have been completed]

Once the second test has finished, the log file would look like this:

Results:
Test 1 Result A: Passed
Test 1 Result B: 1.5 Volts
Test 2 Result A: Passed
Test 2 Result B: 2.0 Volts

Log:
Setting up instruments.
Beginning test 1.
[Steps in test 1]
Finished test 1.
Beginning test 2.
[Steps in test 2]
Finished test 2.
All tests complete.

How would I go about doing this? I've been looking at the help files for QFile and QTextStream, but I'm not seeing a way to insert text in the middle of existing text. I don't want to create separate files and merge them at the end because I'd end up with separate files in the event of a crash. I also don't want to write the file from scratch every time a change is made because it seems like there should be a faster, more elegant way of doing this.

SharpHawk
  • 378
  • 7
  • 19

4 Answers4

2

QFile.readAll will read the entire file into a QByteArray. On the QByteArray you can then use insert to insert text in the middle, and then write it back to file again.

Or you could use the classic c style that can modify files in the middle with the help of filepointers.

Johan
  • 20,067
  • 28
  • 92
  • 110
  • does reading it into the QFile actually remove that text from the file? can you give an example to write it back to the file again so that the file now contains the previous text plus the modifications? thanks! – Rachael Feb 16 '15 at 22:16
2

As @Roku pointed out, there is no built in way to insert data in a file with a rewrite. However if you know the size of the region, i.e., if the text you want to write has a fixed length, then you can write an empty space in the file and replace it later. Check this discussion in overwriting part of a file.

Community
  • 1
  • 1
rpsml
  • 1,486
  • 14
  • 21
  • Yes, that would work, but I wouldn't recommend that. The output files contaning a lot of empty, "reserved", space would look awful and a very simple logging code could turn into a needlessy complicated code. –  Aug 14 '12 at 19:32
1

I ended up going with the "write the file from scratch" method that I mentioned being hesitant about in my question. The benefit of this technique is that it results in a single file, even in the event of a crash since the log and the results are never placed in different files to begin with. Additionally, rewriting the file only happens when adding new results (an infrequent occurrence), whereas updating the log means simply appending text to the file as usual. I'm still a bit surprised that there isn't a way to have the OS insert text into a file for you.

Oh, and for those of you who absolutely must have this functionality as efficiently as possible, the following might be of use: http://www.codeproject.com/Articles/17716/Insert-Text-into-Existing-Files-in-C-Without-Temp

SharpHawk
  • 378
  • 7
  • 19
0

You just cannot add more stuff in the middle of a file. I would go with two separate files, another for the results and another for the logs.