Text file I/O
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This file should have exactly three a-s..."
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt') %gets the file
z=0;
charnum=0;
if fid < 0 || ~ischar(character) %checks if its valid, but I don't know how to return an "-1" if file missing
charnum=-1
end
escrito = fgets(fid)
for i=1:length(escrito)
z=z+1;
if escrito(z)==char(character) %Here I check every position of the text to see if it's equal to the character
charnum=charnum+1; %It sums everytime it happens
end
end
charnum %returns the sum
fprintf('\n');
fclose(fid);
end
The code works in the basic files, but fails at:
"Test with all visible characters
Variable charnum has an incorrect value. When testing with ' ' your solution returned 1 which is incorrect. (75444)"
And the "Non existent file" thing
0 Commenti
Risposta accettata
Voss
il 27 Mar 2022
Try this (and see the comments for explanation):
function charnum = char_counter(fname,character)
fid = fopen(fname); % opens the file for reading
% check that the file is valid, that 'character' is in fact a
% character array (ischar), and that it is a single character (isscalar)
if fid < 0 || ~ischar(character) || ~isscalar(character)
charnum=-1;
return % return here, i.e., you cannot do the search in this case
end
% read the entire file, store the contents as a character row vector
% (there are plenty of other functions you can use to read a text file
% instead of fread)
escrito = fread(fid,'*char').';
fclose(fid);
% escrito == character is an array of logicals (true/false), one element
% per character in the file, with value 'true' where that character is the
% character you want to count.
% nnz is number of non-zero elements; when used with a logical array, it
% returns the number of elements that are 'true', so that's the count.
charnum = nnz(escrito == character);
% % this following way works too, except you don't need to use char() to convert
% % 'character' to a character; it already is a character - this was checked
% % above using ischar()
% z = 0;
% charnum = 0;
% for i=1:length(escrito)
% z=z+1;
% if escrito(z)==char(character) %Here I check every position of the text to see if it's equal to the character
% charnum=charnum+1; %It sums everytime it happens
% end
% end
end
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!