r+ and text file length
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I had asked in a previous question https://www.mathworks.com/matlabcentral/answers/406987-fprintf-leaving-remainder-of-line about fprintf leaving text on a new line. I was opening a file using r+ before changing text and rewriting the file. I have noticed since then if I change the text in a manner which changes the length of the text file (ex apple vs bosenberry), it will leave text in a new line.
I have been able to get around this by closing the file then reopening it in w+ before using fprintf to rewrite the file then closing it at the end of the function. Is it a limitation of r+ that it is bound by the length of the text file or is there a way to get around it without having to close and reopen the file?
1 Commento
dpb
il 25 Giu 2018
That was the point Jan and Walter made in the previous posting -- reopening/reposition a file does not delete any existing content of the file so that if you make changes and rewrite data that is shorter than what was there before, everything from the last character you write forward is still in the file.
If you want entirely new content in the file, the only reliable was in Matlab is to close and reopen it. Otherwise, you'll get the desired result if the new file content is as long or longer than the original, but if shorter, then you get the result you see.
It was possible in the original code you showed to have computed the wrong size but that is, while a coding error, a diversion from the more fundamental issue.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!