MATLAB Answers

text file I/O

226 views (last 30 days)
asad jaffar
asad jaffar on 20 Apr 2019
Commented: Walter Roberson on 28 Sep 2020
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..."
charnum = char_counter('simple.txt','a')
charnum =
3
function charnum = char_counter(fname,character)
fid=fopen(fname);
if fid< 0
charnum = -1;
return;
end
if strcmp(character,'')==1
charnum=0;
return;
end
if double(character)>=35 && double(character)<=43 && double(character) ~=39 && double(character) ~= 41 && double(character) ~= 40
charnum = 0;
return;
end
if double(character) >=60 && double(character) <=64 && double(character) ~= 63
charnum = 0;
return;
end
if double(character) == 81 || double(character) == 88 || double(character) == 90
charnum = 0;
return;
end
cc = fgets(fid);
sumv=0;
while ischar(cc)
z = sprintf('%s',cc);
k = strfind(z,character);
sumv = sumv + length(k);
cc = fgets(fid);
end
charnum = sumv;
if charnum == 0
charnum =-1;
return;
end
can you guys tell whats wrong with this code?

  2 Comments

Geoff Hayes
Geoff Hayes on 20 Apr 2019
asad - I'm not sure what your question is. If I run the code on a file that contains the line "This file should have exactly three a-s...", then the answer (from the function) is three...which is correct. What response or error are you observing?
asad jaffar
asad jaffar on 20 Apr 2019
@Geoff hayes i am facing a problem for this example
Test with all visible characters
Variable charnum has an incorrect value. When testing with '' your solution returned -1 which is incorrect. (0).
can you tell me what can i do ?

Sign in to comment.

Answers (11)

Jiaqi Liu
Jiaqi Liu on 3 Sep 2019
function charnum = char_counter(frame,character)
fid = fopen(frame,'r');
if fid < 0
charnum = -1;
return
end
if ischar(character) == 0
charnum = -1;
return
end
inhalt = fscanf (fid, '%c');
charnum = count(inhalt,character);
end

  2 Comments

Aniket Bordekar
Aniket Bordekar on 15 Sep 2020
Why is %c used?
Walter Roberson
Walter Roberson on 15 Sep 2020
fscanf() without a count asks MATLAB to read as far as possible in the file that still matches the given format. The format given is %c, which matches any one character, and since every byte stored in the file can be interpreted as a character, the result of using fscanf with '%c' format and no count, is to read the entire rest of the file one character at a time.
... Some day I should test with R2020a or later, and a byte stream that does not represent valid UTF8, to see whether %c would stop reading at that point...

Sign in to comment.


Muhammad Sadiq
Muhammad Sadiq on 7 May 2020
Edited: Muhammad Sadiq on 7 May 2020
function charnum = char_counter(frame,character)
fid = fopen(frame,'r');
if fid < 0
charnum = -1;
return
end
if ischar(character) == 0
charnum = -1;
return
end
inhalt = fscanf (fid, '%c');
charnum = count(inhalt,character);
end

  1 Comment

Ankit Meena
Ankit Meena on 24 Jun 2020
its working

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Apr 2019
We already explained. If you are passed something that is a character but the character is not found in the file then you need to return 0 not -1. Only return -1 if the file does not exist or what you are passed is not character data type.

  2 Comments

asad jaffar
asad jaffar on 20 Apr 2019
@walter roberson can you tell me the line number where i have to return 0 and not -1 , i mean to say at which line do i have to make changes?
Walter Roberson
Walter Roberson on 20 Apr 2019
Line 33.

Sign in to comment.


Fazlul  Haque
Fazlul Haque on 15 May 2020
done with fgets and while loops

  1 Comment

Walter Roberson
Walter Roberson on 15 May 2020
If the file is valid but charv is not a character, then you leave the file open when you return.
We recommend against using sum as the name of a variable: it is very common to get confused and also want to use sum() as the name of the summation function in the same code where you use sum as a variable.

Sign in to comment.


Arup Kumar Debnath
Arup Kumar Debnath on 1 Jun 2020
I was trying to answer this question. But i stuck there, Can any one help me and find the fault in this code?
Disclaimer= I am a noob here.
function charnum=char_counter(fname,character)
fid=fopen(fname,'rt');
if fid<0
error('error opening file\n');
charnum=-1;
return;
end
total=fgets(fid);
ii=1;
a=size(total);
charnum=0;
while ii<=a
if total(ii)=='character';
charnum=charnum+1;
else
ii=ii+1;
end
end

  1 Comment

Walter Roberson
Walter Roberson on 1 Jun 2020
you do not validate that the second input is a character.
You are comparing the input to the literal character vector ['c' 'h' 'a' 'r' 'a' 'c' 't' 'e' 'r'] instead of to what the input variable named character holds.
You are not incrementing ii if the comparison is true.

Sign in to comment.


SHASHANK SINGH
SHASHANK SINGH on 7 Jun 2020
function charnum = char_counter(fname, character)
fid = fopen(fname,'r');
if fid < 0 || ischar(character) == 0
charnum = -1;
return
end
inhalt = fscanf(fid, '%c');
charnum = count(inhalt, character);
end

  1 Comment

Walter Roberson
Walter Roberson on 7 Jun 2020
If the fname is valid but character is not a character, then you leave the file open when you return.

Sign in to comment.


Pranav Mishra
Pranav Mishra on 8 Jun 2020
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt');
if (fid < 0)
charnum = -1;
return;
end
if (ischar(character) ==0)
charnum = -1;
else
c = 0;
oneline = fgets(fid);
while ischar(oneline)
character_line = char(oneline);
[a b] = size(strfind(character_line,character));
c = c + b;
oneline = fgets(fid);
end
charnum = c;
end
fclose(fid);
end

  1 Comment

Walter Roberson
Walter Roberson on 8 Jun 2020
Note that the result of fgets() is already a character vector (unless end of file), so you do not need to use char() on oneline.

Sign in to comment.


Ujjawal Barnwal
Ujjawal Barnwal on 8 Jun 2020
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt');
charnum = 0;
if fid < 0 || ~ischar(character)
charnum = -1;
return;
end
oneline = fgets(fid);
while ischar(oneline)
a=length(findstr(oneline,character));
charnum = charnum + a;
oneline = fgets(fid);
end
fclose(fid);
end

  1 Comment

Walter Roberson
Walter Roberson on 12 Jun 2020
If the fname is valid but character is not a character, then you leave the file open when you return.

Sign in to comment.


Hari Kiran Tirumaladasu
Hari Kiran Tirumaladasu on 21 Jun 2020
function charnum = char_counter(fname,character)
fid = fopen(fname,'r');
if fid < 0 || ischar(character) == 0
charnum = -1;
error('The file entered is either incorrect or the character entered could not be found');
return;
end
incount = fscanf(fid,'%c');
charnum = count(incount,character);

  0 Comments

Sign in to comment.


Adityesh Satam
Adityesh Satam on 23 Jul 2020
I was able to get all correct using this code:
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt');
charnum = 0;
ii = 0;
if fid < 0
charnum = -1;
return;
end
if ischar(character) == 0
charnum = -1;
end
% Reading a file one line at a time
one_line = fgets(fid);
while ischar(one_line)
for ii = 1:length(one_line)
if strcmp(one_line(ii),character) && ischar(character)
charnum = charnum + 1;
end
end
one_line = fgets(fid);
end
fclose(fid);
end

  1 Comment

Walter Roberson
Walter Roberson on 23 Jul 2020
If the character parameter is not a valid parameter, you go ahead and read the file anyhow, but each time testing again that ischar(character) is true. That is a waste of time. If ischar(character) is false then do not bother reading the file.

Sign in to comment.


Sachin Patare
Sachin Patare on 27 Sep 2020
function charnum=char_counter(fname,character)
fid=fopen(fname,'rt');
if fid<0
charnum=-1;
return
end
x=fileread(fname);
if ischar(character)==1
a=findstr(x,character);
b=size(a);
charnum=b(1,2);
else
charnum=-1;
end

  3 Comments

Walter Roberson
Walter Roberson on 27 Sep 2020
How many files are open after the code returns?
Jan
Jan on 27 Sep 2020
If you use fileread, you can omit the fopen. If you use fopen, do not forget to close the file with fclose.
numel() is easier than getting the 2nd output from size. ischar replies a logical already, so there is no need to compare it with "== 1".
findstr is deprecated for more than 10 years now. Use strfind instead.
function charnum=char_counter(fname, character)
charnum = -1; % Default value
if exist(fname, 'file') == 2 % Check existence of file
s = fileread(fname);
if ischar(character) && isscalar(character)
charnum = sum(s == character);
% Or charnum = numel(strfind(s, character));
end
end
Walter Roberson
Walter Roberson on 28 Sep 2020
The one advantage of testing the fopen() over testing file existence with exist, is that fopen() will fail smoothly if the file exists but you do not have permission to read it, or the file exists but it is locked so you cannot read it at the moment. In either of those cases, fileread() would generate an exception that would have to be handled with a try/catch to replicate the fopen() ability to detect that the file cannot be read.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by