How to make sure completion of a write to file
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Within a function B (which is called from another function A) I am using multiple fprintf commands to print a matrix to a file. The file is opened and closed inside the function B. In faster machines it happens that the matrix does not finish printing, however the function B terminates and gives back control to the calling function A.
I have noticed that despite the fclose commmand at the end of function B, the file is still open when controls returns to calling function A. And if A does not close the file I can stil write to it or close it from the Matlab console. If I do that (closing from console) then all missing data is flushed to the file, but if I don't (for example if I quit Matlab) then a piece of the matrix will never be printed. Opening as 'w' or 'W' does not make any difference.
I have found a workaround: opening in A and passing the file id as parameter to function B, let function B do the printing and closing the file in A. This way insures that the whole matrix is printed, but it is very inconvenient because the function B should print in a large number of files (names changing all the time) and this workaround limits my ability to use it as expected.
I would appreciate some help.
0 Commenti
Risposte (2)
Walter Roberson
il 9 Ago 2012
In theory, if you perform any fseek() operation on the output file, it should flush. At least that is what the standards say for the underlying POSIX fseek() operation; I am not certain that it works that way for MATLAB.
Could I suggest that you experiment with using an onCleanup() to close the file? That would catch any cases where the function somehow returned early before reaching the fclose() in your logic.
2 Commenti
Walter Roberson
il 9 Ago 2012
fseek() is not just for read modes: you can fseek() on any stream. fseek() to 0 bytes relative to current position if you need to.
onCleanup() has nothing to do with code composer. The "on" is part of the name.
Vedere anche
Categorie
Scopri di più su Low-Level File I/O in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!