r+ and text file length
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
dpb
le 25 Juin 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.
Réponses (0)
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!