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

4 次查看(过去 30 天)
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.

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2023-9-25
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 个评论
Walter Roberson
Walter Roberson 2023-9-25
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by