Effacer les filtres
Effacer les filtres

Replace lines in text file with different length line

1 vue (au cours des 30 derniers jours)
Andrei
Andrei le 22 Avr 2014
Réponse apportée : Andrei le 23 Avr 2014
Hi! I have a text file,something like that:
var1 = 1;
var2 = 20;
I have to replace the values after '=',but,if i replace '1' with '100', for example, text file looks like this: var1 = 100;var2 = 20; If i put '\n', text file look like this:
var1 = 100;
ar2 = 20;
I want it to look like:
var1 = 100;
var2 = 20;
I get the line with fgets,position with ftell, modify the line, go to the correct position in file with fseek, and then print the modified line with fprintf. Works if instead of '1' i want to put '3',for example,because they are the same length. I guess the string looks like: var1 = 1;\nvar2 = 20;,and when i replace '1' with '100' overwrites the string. Any ideea how can i do that?

Réponses (2)

Joseph Cheng
Joseph Cheng le 22 Avr 2014
You'll have to read in the whole file after where you want to insert and re-write it. At least to my knowledge there isn't a built-in matlab function to insert a chunk of data into a file and have everything after it shift over.

Andrei
Andrei le 23 Avr 2014
Ok,thank you. I don't want do to that because the file is big,but I guess there is no other way. I tried to add empty rows in file,but it's the same behaviour...

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by