How do I delete a specific line from a text file?
Afficher commentaires plus anciens
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
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!