fopen does not open a file correctly
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alessandro
el 24 de Mzo. de 2025
Comentada: Alessandro
el 26 de Mzo. de 2025
I'm trying to implement in my code some code written by someone else but i'm having problem with the "fopen" function as it doesnt open correctly the file even if the path is correct.
I should add that all these files are in the same directory which contains the subdirectories .mpropep (after it is created), cpropep, io, gasdynamics and num. The function mpropepWriteInput.m is saved inside io
(I'm really sorry if i include more code than necessary but i'm new to this)
my main code is:
addpath("cpropep")
addpath("io")
addpath("gasdynamics")
addpath("num")
pressure=5e05; %TEST VALUE
expansionRatio=2; %TEST VALUE
mpropepPath = pwd; % mpropep initialization
mkdir .mpropep;
%I included some checks
if exist('.mpropep', 'dir') ~= 7
error(' Failed to create .mpropep folder. Check permissions.');
else
disp(' .mpropep folder created successfully.');
end
fid = fopen(fullfile(mpropepPath, '.mpropep', 'test.txt'), 'w');
if fid == -1
error(' Cannot create file. Check permissions.');
else
fprintf(fid, 'Test file created successfully.\n');
fclose(fid);
disp(' File written successfully.');
end
%end of checks
oxidantID = 657; % Dinitrogen monoxide
fuelID = 1032; % Paraffin
OFRatio = 7;
listPropellantID = [oxidantID fuelID];
listMass = [OFRatio 1];
mpropepWriteInput(listPropellantID, listMass, 'EQ_AR', pressure, expansionRatio, '')
runComputation('', '', mpropepPath);
output = readOutputFile('');
characteristicVelocity = getParamFromOutput(propellantNumber, 'c*', 'EQ_AR', output);
the part in the function mpropepWriteInput that is giving me problems is:
function [] = mpropepWriteInput( ...
listPropellantID, ...
listPropellantMass, ...
solver, ...
solverArg1, ...
solverArg2, ...
path)
% Number of components in the simulation
n = length(listPropellantID);
% Check that components are enough
if n < 2
error('Components must be at least two. Insert component twice if alone.')
end
% Check that lists are same size
if length(listPropellantID) ~= length(listPropellantMass)
error('Number of IDs and masses does not match')
end
ciao1=1; %this is just a flag I included to check where code was going
disp(ciao1);
% Open input file
fileattrib('.mpropep', '+w'); % Grant write permissions
ciao2=2;
disp(ciao2);
if ~exist("path","var")
inputFile = fopen('.mpropep/input.txt','w');
ciao3=31;
disp(ciao3);
else
inputFile = fopen(path,'w');
ciao3=32;
disp(ciao3);
end
ciao4=4;
disp(ciao4);
disp(inputFile);
ciao5=5;
disp(ciao5);
% Premise
fprintf(inputFile,'### AUTOMATICALLY GENERATED: EDIT AT OWN RISK ###\n');
time = clock;
fprintf(inputFile,'## run on %i/%i/%i %i:%i:%i\n\n',time(1),time(2),time(3),time(4),time(5),round(time(6)));
the problem that I when I run the code I get:
Warning: Directory already exists.
> In Altitude_1D_Sounding_Rocket (line 42)
.mpropep folder created successfully.
File written successfully.
1
2
32
4
-1
5
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in mpropepWriteInput (line 89)
fprintf(inputFile,'### AUTOMATICALLY GENERATED: EDIT AT OWN RISK ###\n');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in Altitude_1D_Sounding_Rocket (line 64)
mpropepWriteInput(listPropellantID, listMass, 'EQ_AR', pressure, expansionRatio, '')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Mzo. de 2025
Movida: Walter Roberson
el 24 de Mzo. de 2025
You should be using
if ~exist("path","var")
infilename = '.mpropep/input.txt';
[inputFile, msg] = fopen(infilename,'w');
if inputFile < 1
error('failed to open "%s" for writing because "%s"', infilename, msg);
end
ciao3=31;
disp(ciao3);
else
[inputFile, msg] = fopen(path,'w');
if inputFile < 1
error('failed to open "%s" for writing because "%s"', path, msg);
end
ciao3=32;
disp(ciao3);
end
Note that using a variable named path is not the best idea, as doing so interferes with the path() function call.
4 comentarios
Walter Roberson
el 24 de Mzo. de 2025
You have several conditions that are redundant. You can simplify to:
outputpath = '.mpropep';
if ~isdir(outputpath)
mkdir(outputpath);
end
fileattrib(outputpath, '+w'); % Grant write permissions
% Open input file
if ~exist('inputPath', 'var') || isempty(inputPath)
inputPath = fullfile(outputpath, 'input.txt'); % Default inputPath
end
[inputFile, msg] = fopen(inputPath,'w');
if inputFile < 0
error('failed to open "%s" for writing because "%s"', inputPath, msg);
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Test and Measurement en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!