How to change certain lines in a file?

Hello, I have a text file:
f2.txt:
1
2
3
4
5
6
7
8
9
10
11
Then, I want to change certain lines of this text file with another condition.
clc;
close all;
clearvars;
%declaring some variables
a=0;
b=1;
c=0;
d=1;
e=0;
f=1;
fin = fopen('f2.txt','r+'); %opening text file f2.txt in w+ mode
lcounter = 0; %lcounter is a line counter variable
while (lcounter<12)
nextline = fgetl (fin); %fgetl() gets nest line of the fin in nextline var
if (lcounter ==1 && a==0) %If both conditions are satisfied, then write **L1 to file
nextline = ['**L1'];
elseif (lcounter ==2 && b==0)
nextline = ['**L2'];
elseif (lcounter ==3 && c==0)
nextline = ['**L3'];
elseif (lcounter ==4 && d==0)
nextline = ['**L4'];
elseif (lcounter ==5 && f==0)
nextline = ['**L5'];
end
lcounter = lcounter + 1;
end
fclose (fin);
Here is the code. What I am trying to do is if both conditions are satisfied, I want to write something to the file. While debugging, I see control goes to the writing statement, but nothing is written to the file. Any idea how to do this?

5 Comments

per isakson
per isakson on 16 Apr 2015
Edited: per isakson on 16 Apr 2015
" writing statement" &nbsp I cannot see any fprintf statement or similar
" Any idea how to do this?" &nbsp read one line at a time from f2.txt and write to a new file
I wanted to modify the same file without writing another file. What can I change in code to get statements written to file?
per isakson
per isakson on 16 Apr 2015
Edited: per isakson on 17 Apr 2015
"I wanted to modify the same file without writing another file" &nbsp That is not possible if it requires that existing "bytes" in the file are "moved". With memmapfile you may overwrite characters of the file as long as you don't change the size of the file.
BTW, what do you mean by "without writing another file"? What about
  1. read the entire file to a string variable, str
  2. modify str
  3. overwrite the old file
How can I 'read' entire file into a string variable and write that variable back?
Or
  1. read one line at a time from f2.txt
  2. modify the line as needed
  3. write the line to a new file, temp.txt
  4. and finally delete f2.txt and rename temp.txt to f2.txt

Sign in to comment.

Answers (1)

Ken Atwell
Ken Atwell on 17 Apr 2015
As Per states, you can't edit an existing file unless the changes are exactly the same size (number of bytes) as the original, which is almost never the case in a text file. I know text editors give the illusion you're only editing individual lines, but in fact the entire file is likely being rewritten on save.
Honestly, I would write a second file line by line. Start with what you have: Read a line, maybe change it, and write the line to a second file that you fopen (with 'w') just before your for loop. You can always replace the original file with the new file when you're done with copyfile or similar -- but don't do that until your code is throughly debugged, or you'll keep clobbering your input data.

2 Comments

Why we cannot do reading and replacing the same file without writing a new file? Are there any OS limitations? Can you comment?
Ken Atwell
Ken Atwell on 18 Apr 2015
Edited: Ken Atwell on 18 Apr 2015
Because most files, and especially plain text files, are just a long sequence of numbers (bytes). If you insert any new byte, every number after the insertion point must be pushed "to the right". If you delete a byte, everything after must be pulled "to the left".
So you're left with the choice of rewriting the file with every modification (ugh), or "stream" from an input file to an output file like Per and I have suggested.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Tags

Asked:

on 16 Apr 2015

Edited:

on 18 Apr 2015

Community Treasure Hunt

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

Start Hunting!