How to address: Invalid file identifier. Use fopen to generate a valid file identifier?

4 views (last 30 days)
Hello,
I am trying to run the following lines but it shows error.
fid = fopen('/dev/tty', 'r');
% Display program information
fprintf(fid, '\nDetails of the program.\n');
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
I googled answer for it and understood that since fid is -1, I cannot write. But, I do not know how to solve this issue.
Any help is greatly appriciated.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 25 Sep 2023
fid = fopen('/dev/tty', 'r');
if fid == -1
error('Failed to open /dev/tty for writing.');
end
fprintf(fid, 'Details of the program.\n')
#else
% Open for writing (r for reading)
fid = fopen('/dev/tty', 'w');
  1 Comment
Walter Roberson
Walter Roberson on 25 Sep 2023
device = '/dev/tty';
if ~exist(device, 'file')
error('no file "%s"', device);
else
[fid, msg] = fopen(device, 'a+');
if fid == -1
error('Failed to open "%s" because: "%s"', device, msg);
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!