A file “potfilenames.dat” stores potential file names, one per line. The names do not have any extension. Write a script that will print the names of the valid files, once the extension “.dat” has been added
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:9| Question number:10.9
All Answers
total answers (1)
Ch9Ex10.m
fid = fopen('potfilenames.dat');
if fid == -1
disp('File not opened')
else
count = 0;
while ~feof(fid)
aname = fgetl(fid);
aname = strcat(aname,'.dat');
tryit = fopen(aname);
if tryit ~= -1
count = count + 1;
fprintf('%s is a valid file\n', aname)
fc = fclose(tryit); % Assume this works
end
end
closeres = fclose(fid); % Assume this works
fprintf('\nThere were %d valid file names\n', count)
end
need an explanation for this answer? contact us directly to get an explanation for this answer