Q:

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

0

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. “Valid” means that the file exists in the Current Directory, so it could be opened for reading. The script will also print how many of the file names were valid. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now