How do I delete a specific line from a text file?

Hi there guys,
I have a list of file locations that I have saved in a .txt file, all on new lines, like so:
C:\\User\Documents\file_name
C:\\User\Documents\file_name_2
C:\\User\Documents\file_name_3
C:\\User\Documents\file_name_4
I want to look through the file and see if the name in my .txt matches a predetermined name that I have previously chosen, and then delete it.
I can go through and find the one I want to delete with this code:
fileID = fopen('file_names.txt','wt');
num = length(importdata('Possiblelocs.txt');
for ii = 1:num
check = fgets(fileID(ii));
if check == new_str
'You found it!'
break;
else
continue;
end
end
However, I don't think this is a particularly good way of finding it, nor do I know how to delete the entire line when I have. Could anyone point me in the right direction? Thank you!

 Réponse acceptée

dpb
dpb le 27 Juin 2016
It's a sequential file so there isn't any way trivially to do what you want to the file itself. The right way would be to read the file, find the row in memory, delete it still in memory, then rewrite the file.
fileData=textread('file_names.txt','%s','delimiter','\n','whitespace','');
ix=~cellfun(@isempty,strfind(fileData,newstr));
fileData(ix)=[];
fid=fopen('file_names.txt','w');
for i=1:length(fileData)
fprintf(fid,'%s\n',fileData{i});
end
fid=fclose(fid);

6 commentaires

Faith Huynh
Faith Huynh le 27 Juin 2016
Modifié(e) : Faith Huynh le 27 Juin 2016
Aw man, I was hoping there was a faster solution.
Question, though: this leaves a \n on the last entry in this file-- is there any way I can make it not have a new line at the end of the .txt?
Thank you!! :)
dpb
dpb le 27 Juin 2016
Modifié(e) : dpb le 27 Juin 2016
That's far faster than reading a file record-by-record will be...so if it solves the problem, "Accept" the Answer for a minimum to let folks know it's closed.
dpb
dpb le 27 Juin 2016
"...is there any way I can make it not have a new line at the end of the .txt?"
Sure, just change the format string for the last record. BUT, that leaves an ill-formed file with an incomplete record that may cause difficulties later. All-in-all, I would recommend against doing so. "Just because you can do something, doesn't necessarily mean you should."
Faith Huynh
Faith Huynh le 28 Juin 2016
Hey dpb,
Please excuse my ignorance; I don't do a lot of coding. Why does that leave an illformed file? I'm using the .txt file to read in data to a listbox, and if there's an extra space at the end it messes up the listbox display.
dpb
dpb le 28 Juin 2016
Modifié(e) : dpb le 28 Juin 2016
I "don't do windows", so I'm certainly nobody to ask details re: GUI components, but isn't that only a function of the 'position' height? The 'String' property is what it is; there shouldn't be an empty full record and the linefeed shouldn't be in the string you display--mayhaps that's the problem; you're reading the lines including record terminators, not just the data???
Oh, and as for the "why?" question, the answer is that the final record doesn't have a record terminator and that is irregular. If you were to, say, append to the file you would have to prepend a newline before the first new record is written or it will simply be tacked onto the existing last record--unlikely what would be intended is just one example. Some applications will not correctly read the final record if it isn't terminated. It's "just not right" that way.
dpb
dpb le 29 Juin 2016
"... an extra space at the end"
Oh, just dawned on me...certainly a blank line would show up, but that's not what speaking of when adding just the terminating \n. There should be no extra record unless it's in your original file and is simply being copied--if that's the case, clean up the original.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by