Try to use:
var
f: TextFile;
...
AssignFile(f,fullpath);
...
CloseFile(f);
And see if it works.
Edit: Missed some code.
This code is from SmartPascal.
Setting the {$i-} only prevents your program from crashing, it does not ignore the IOresults..
If there is a lingering IO error, these functions will be not execute.
So at some point in code, you may have had an error and has set the InOutRes variable to something other than 0.
you should always test IoResult at some point which will report the current error and also clear it.
Allright, so I turned on my computer today and amended the code:
function TToroidCoil.SaveToFile(fullpath:string):Word;
var f:text;i:word;r:word;
begin
assign(f,fullpath);
{$I-}
rewrite(f);
{$I+}
r:=IOResult;
if r<>0 then begin SaveToFile:=r;exit;end;
writeln(f,'# Coil Parameters');
//
close(f);
SaveToFile:=0;
end;
and then the caller.
rocedure TForm1.btnSaveAsOBJClick(Sender: TObject);
var filename:string;i,reply:integer;error:word;
begin
SaveDialog1.Title:='Save coil as .OBJ file';
if SaveDialog1.Execute then
begin
Filename := Savedialog1.filename;
if pos('.', Savedialog1.filename) > 0 then
begin
I := pos('.', Savedialog1.filename);
if uppercase(Copy(Savedialog1.filename, I + 1, 3)) <> 'OBJ' then
filename := copy(filename, 1, I) + 'obj';
end
else
begin
filename := savedialog1.filename + '.obj';
end;
if FileExists(filename) then reply:=MessageBox(0,'File exists! Overwrite ?','Save as .OBJ',MB_YESNO);
end else reply:=IDYES;
if reply=IDYES then begin
error:=MyToroid.SaveToFile(filename);
if error<>0 then ShowMessage('An Error Occured!' + #13 +' (error code '+int2str(error,0)+')');
end;
end;
Ran the program, told it to rewrite the file it made last night. Bang, error 5, access denied.
It was so denied I couldn't even delete it manually from Total Commander!
Now, I understand there is something funny.
I remember encountering similar problems when I was coding in FoxPro.
FoxPro has the FOPEN() function, which doesn't ever work in a rewrite mode.
I mean if you read the documentation here (
https://hackfox.github.io/section4/s4g194.html ) you might say, "Well, we just do a fhandle=FOPEN(filename,1)"
But that doesn't work.
For this reason, I had to write a zillion years ago a workaround to restore file functions functionality the way did work in BP 7 at that time.
procedure AssignReset
parameters filename,filehandle
filehandle=FOPEN(filename,0)
return
procedure AssignRewrite
parameters filename,filehandle
if file(filename)==.F.
filehandle=FCREATE(filename,0)
else
set safe off
delete file (filename)
set safe on
filehandle=FCREATE(filename,0)
endif
return
procedure AssignAppend
parameters filename,filehandle
if file(filename)==.F.
filehandle=FCREATE(filename,0)
else
filehandle=FOPEN(filename,0)
=FSEEK(filehandle,FileSize(filehandle))
endif
return
Problem is, this workaround doesn't work here in Pascal. If I check if FileExists() and do assign(f);erase(f); then this doesn't work as well.