Failure of fopen for a text file I have full access upon
3 views (last 30 days)
Show older comments
Hello,
Before highlighting my problem, I'm describing my code, and the problem is explained at the end of the request.
My program does the following :
1 - From a side program, I generate a text file containing data that I want to modify : so I open this text file using the following lines of codes.
Input = fopen(fullfile('C:\Users\Lucas\Desktop\MonDossier','inputdata.txt'));
if Input==-1
error('Failed to open the file because: %s', message);
else
tlineSP = fgetl(Input);
tlinesSP = cell(0,1);
while ischar(tlineSP)
tlinesSP{end+1,1} = tlineSP;
tlineSP = fgetl(Input);
end
end
fclose(Input);
The entities tlineSP and tlinesSP are respectively cell and cell array of characters.
2 - Then I bring modifications to the text file mentioned above, by using a template of the lines I'm interested in and filling it with the values I'm modifying, like so :
for i=1:Etage
%Lis les valeurs de poids/étage et les incrémente.
ligne = ' %u 3000.000 %u.000';
floor=char(tlinesSP(1167+i,1));
Fpoids = str2double(floor(28:32));
Fpoids = Fpoids + Increment;
cellule = [i Fpoids];
tlinesSP{1167+i,1}=sprintf(ligne, cellule(1), cellule(2));
%Modifie les 16 blocs de valeurs correspondant aux poids à chaque noeud.
token = ' %u %u %u %u';
tlinesSP{1178+5*i} = sprintf(token, NouveauNoeud, NouveauNoeud*2, NouveauNoeud*2, NouveauNoeud);
tlinesSP{1179+5*i} = sprintf(token, NouveauInter, NouveauInter*2, NouveauInter*2, NouveauInter);
tlinesSP{1180+5*i} = sprintf(token, NouveauInter, NouveauInter*2, NouveauInter*2, NouveauInter);
tlinesSP{1181+5*i} = sprintf(token, NouveauNoeud, NouveauNoeud*2, NouveauNoeud*2, NouveauNoeud);
end
So the idea is to get the values at their locations in the lines I'm focusing on, and then modify them in the cell array. NouveauInter and NouveauNoeud are both int64.
3 - And finally, I'm writing the cell array lines back into their origin text file, with this :
[Input, message] = fopen(fullfile('C:\Users\Lucas\Desktop\MonDossier','inputdata.txt'),'wt');
if Input==-1
error('Failed to open the file because: %s', message); %This is line 119
else
for i = 1:numel(tlinesSP)
fprintf(Input,'%s\n', tlinesSP{i});
end
end
fclose(Input);
After this I launch the side program with a command line, also written in this very code, which uses the text file inputdata.txt that I modified, and gives as a result output data that does not interfere with the rest of my code.
PROBLEM:
The whole code is looping with a while loop, and after one iteration of the loop, the program stops and I get the following message : Error using Pushdown (name of the program) (line 119) - Failed to open the file because : Permission denied.
The line mentioned here is the writing part of my program, highlighted by the comment 'This is line 119' on part 3 of my request.
Also note that I'm working on my personal space and that I am administrator and only physical user of my computer, I also checked the avaibality of the file and the folder it is contained in, and I have full access on it.
Looking forward your answers, and hoping it will help me solve the problem.
Best regards and in advance, thank you.
Lucas
5 Comments
Walter Roberson
on 7 Feb 2019
your supervisor is correct .
note the first & should be ; or else the cd might not be done in the same process as the executable .
Answers (1)
Jan
on 31 Jan 2019
Edited: Jan
on 31 Jan 2019
The message is partially clear: "Failed to open the file because : Permission denied"
You cannot open the file for writing, but what is the missing "permission"? Usually this means, that the file is opened by Matlab or another program. You can use the free ProcessExplorer of SysInternals to check, which program locks the file.
You can check inside Matlab, if the file is still open:
OpenFID = fopen('all');
for k = 1:numel(OpenFID)
[filename, permission] = fopen(OpenFID(k));
fprintf('Open: %s [%s]\n', filename, permission);
end
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!