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

3 Ansichten (letzte 30 Tage)
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.

Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 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 Kommentar
Walter Roberson
Walter Roberson am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by