Recent

Author Topic: File open or File close - my Question - your Answer ?  (Read 1416 times)

paule32

  • Sr. Member
  • ****
  • Posts: 383
File open or File close - my Question - your Answer ?
« on: March 17, 2025, 10:31:50 pm »
Hello,
I have the given Code.
The Problem I have is, that I get a "File access" problem - file in use.
How can I clode file handles ?

Code: Pascal  [Select][+][-]
  1. procedure foo;
  2. begin
  3. ...
  4.   if not FileExists(filename) then
  5.   begin
  6.     ShowMessage('Error: file: "' + filename + '" could not be found.');
  7.     exit;
  8.   end;
  9.  
  10.   Assign(SourceFile, filename);
  11.   {$I-}
  12.   Reset(SourceFile);
  13.   {$I+}
  14.  
  15.   if IOResult <> 0 then
  16.   begin
  17.     ShowMessage(tr('Error: file: "') + filename + tr('" could not be open.'));
  18.     Close(SourceFile);
  19.     exit;
  20.   end;
  21.  
  22.   Close(SourceFile);
  23.   SendMessage('voodoo programming');
  24. end;
  25.  
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

jamie

  • Hero Member
  • *****
  • Posts: 6869
Re: File open or File close - my Question - your Answer ?
« Reply #1 on: March 18, 2025, 01:25:40 am »
By default, the RESET opens the file for Reading and Writing on untyped files.

You could try setting the
Code: Pascal  [Select][+][-]
  1. FILEMODE :=0;
  2.  

That would put it in read only mode.

jamie
The only true wisdom is knowing you know nothing

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: File open or File close - my Question - your Answer ?
« Reply #2 on: March 18, 2025, 06:57:01 am »
Thanks for reply.

But I get a "File is open" error message.
The second MessageBox will not be displayed.

Code: Pascal  [Select][+][-]
  1. procedure yymain(filename: String);
  2. begin
  3.   if not FileExists(filename) then
  4.   begin
  5.     ShowMessage(tr('Error: file: "') + filename + tr('" could not be found.'));
  6.     yyclear;
  7.     exit;
  8.   end;
  9.  
  10.   filemode := 0;
  11.   Assign(yyinput, filename);
  12.   Reset(yyinput);
  13.  
  14.   Close(SourceFile);
  15.   ShowMessage('Parsing abgeschlossen.');
  16.   exit;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 16781
  • Ceterum censeo Trump esse delendam
Re: File open or File close - my Question - your Answer ?
« Reply #3 on: March 18, 2025, 07:13:55 am »
 Close(SourceFile);??
Must be
 Close(yyinput);

Filemode must indeed be 0
And before reset until after close the code must be in  {$I-} state otherwise IOResult makes no sense.
« Last Edit: March 18, 2025, 07:16:43 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

d2010

  • Full Member
  • ***
  • Posts: 139
Re: File open or File close - my Question - your Answer ?
« Reply #4 on: March 18, 2025, 07:14:54 am »
By default, the RESET opens the file for Reading and Writing on untyped files.

You could try setting the
Code: Pascal  [Select][+][-]
  1. FILEMODE :=0;
  2.  
That would put it in read only mode.
jamie
Please help me?,
C:Q1= You put FILEMODE:=00 before or After that "Assign?"
C:Q2= Why filesize (ftex7) is broken or crash?
         (I fix-it with Str_toFilesize ...)
Code: [Select]
  assign (ftex7,argv0);
  filemode :=000;
  {$I-}
  reset(ftex7, 1 );
  {$I+}
  IF ioresult <> 0 THEN exit...
  if (zprec.size<1) then zprec.size := filesize (ftex7);
  if (zprec.size<1) then  zprec.size:=Str_toFilesize(argv0);
End;
My Code work very good,
« Last Edit: March 18, 2025, 07:19:55 am by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16781
  • Ceterum censeo Trump esse delendam
Re: File open or File close - my Question - your Answer ?
« Reply #5 on: March 18, 2025, 07:18:10 am »
After assign and before reset/rewrite.
(Assign can not generate an IOResult)
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: File open or File close - my Question - your Answer ?
« Reply #6 on: March 18, 2025, 07:23:44 am »
I create the file with:
SynEdit.Lines.SaveToFile('tmp.tmp');

So, file exists

Code: Pascal  [Select][+][-]
  1. procedure yymain(filename: String);
  2. var
  3.   fn: String;
  4. begin
  5.   if not FileExists(filename) then
  6.   begin
  7.     ShowMessage(tr('Error: file: "') + filename + tr('" could not be found.'));
  8.     yyclear;
  9.     exit;
  10.   end;
  11.  
  12.   fn := StringReplace(ExtractFilePath(Paramstr(0)) + filename,
  13.   '/', '\', [rfReplaceAll]);
  14.  
  15.   {$I-}
  16.   Close(yyinput);
  17.   Assign(yyinput, fn);
  18.   filemode := 0;
  19.   Reset(yyinput);
  20.   {$I+}
  21.  
  22.   if IOResult <> 0 then
  23.   begin
  24.     ShowMessage('Error: file: "' + fn + '" could not be open.');
  25.     yyclear;
  26.     exit;
  27.   end;
  28.  
  29.   yyWrap  := @newYYwrap;
  30.   yyclear;
  31.     ShowMessage('Parsing abgeschlossen.');
  32.   yyParse;
  33.  
  34.   Close(yyinput);
  35.   ShowMessage('Parsing abgeschlossen.');
  36. end;

bring me Error - see screen shot.
« Last Edit: March 18, 2025, 07:26:23 am by paule32 »
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

TRon

  • Hero Member
  • *****
  • Posts: 4280
Re: File open or File close - my Question - your Answer ?
« Reply #7 on: March 18, 2025, 07:33:18 am »
bring me Error - see screen shot.
That is an imaginative error invented by yourself. Show the real error. And for that matter, all the relevant declarations.
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 16781
  • Ceterum censeo Trump esse delendam
Re: File open or File close - my Question - your Answer ?
« Reply #8 on: March 18, 2025, 08:25:22 am »
Try this code. It is at least all in the right order:
Code: Pascal  [Select][+][-]
  1. procedure yymain(filename: String);
  2. var
  3.   Err:integer;
  4.   ErrorMsg:String;
  5. begin
  6.   Assign(yyinput, filename);
  7.   {$push}{$I-}
  8.   filemode := 0;
  9.   Reset(yyinput);
  10.   Close(yyinput);
  11.   { store IOResult, because IOResult resets after it is read }
  12.   Err :=IOResult;
  13.  {$pop}
  14.   Case Err of
  15.   0  : ErrorMsg := 'Success';
  16.   2  : ErrorMsg := 'File not found';
  17.   3  : ErrorMsg := 'Path not found';
  18.   4  : ErrorMsg := 'Too many open files';
  19.   5  : ErrorMsg := 'File access denied';
  20.   6  : ErrorMsg := 'Invalid file handle';
  21.   12 : ErrorMsg := 'Invalid file access code';
  22.   102: ErrorMsg := 'File not assigned';
  23.   103: ErrorMsg := 'File not open';
  24.   104: ErrorMsg := 'File not open for input';
  25.   105: ErrorMsg := 'File not open for output';
  26.   else
  27.     { Error codes in appendix D of the users guide }
  28.     ErrorMsg := Format('Error %d occurred',[Err]);
  29.   end;
  30.   ShowMessage(ErrorMsg);
  31. end;
« Last Edit: March 18, 2025, 08:29:32 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: File open or File close - my Question - your Answer ?
« Reply #9 on: March 18, 2025, 11:06:20 am »
Thank you for the reply and the code snippet.
It worked.

When I call the yymain procedure first time I get Success.
When I call the yymain procedure second time I get "File not open"
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Thaddy

  • Hero Member
  • *****
  • Posts: 16781
  • Ceterum censeo Trump esse delendam
Re: File open or File close - my Question - your Answer ?
« Reply #10 on: March 18, 2025, 01:26:03 pm »
When I call the yymain procedure second time I get "File not open"
I can not reproduce that with my code.
Not on Windows nor on Linux.

But you should be able to debug that now.
« Last Edit: March 18, 2025, 01:28:51 pm by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: File open or File close - my Question - your Answer ?
« Reply #11 on: March 18, 2025, 06:48:39 pm »
for informative notices:

on a third party component, I have found a buffer overflow which destroy some frames.
firstly, I had thinking that the on board controls produce my problem - but I was far away from this.

Because a counter produce a loop that was manifested that a bunch of token was not in the range,
and I had switch off range checking.

So, all went in the right pov.
Thanks to all for helping hand's.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

TRon

  • Hero Member
  • *****
  • Posts: 4280
Re: File open or File close - my Question - your Answer ?
« Reply #12 on: March 21, 2025, 11:13:04 am »
Code: Pascal  [Select][+][-]
  1. FUNCTION FileExists ( CONST fname : string ) : boolean;
  2. {simple fileexist function}
  3. VAR ftex : file;
  4.   imjpmig : integer;
  5. BEGIN
  6.     imjpmig := filemode;
  7.    assignfile(ftex,fname);
  8.     filemode:=00;
  9.     {$i-}
  10.     Reset (ftex, 1 );
  11.     filemode :=imjpmig;
  12.     FileExists:=ioresult = 0;
  13.     Closefile(ftex ); IF ioresult <> 0 THEN;
  14.     {$i+}
  15. END;
  16.  
IOResult = o doesn't mean the file exists.

IOResult can only be read once.

origin:
Quote
When the flag is read, it is reset to zero. If IOresult is zero, the operation completed successfully.
Today is tomorrow's yesterday.

paule32

  • Sr. Member
  • ****
  • Posts: 383
Re: File open or File close - my Question - your Answer ?
« Reply #13 on: March 21, 2025, 01:35:31 pm »
Hello sunshines,

I have create a utils.pas unit in which I am layout some helper functions:
Feel free to use it.

Code: Pascal  [Select][+][-]
  1. procedure CheckError(Err: Integer);
  2. var
  3.   ErrorMsg: String;
  4. begin
  5.   case Err of
  6.     0  : ErrorMsg := tr('Success');
  7.     2  : ErrorMsg := tr('File not found');
  8.     3  : ErrorMsg := tr('Path not found');
  9.     4  : ErrorMsg := tr('Too many open files');
  10.     5  : ErrorMsg := tr('File access denied');
  11.     6  : ErrorMsg := tr('Invalid file handle');
  12.     12 : ErrorMsg := tr('Invalid file access code');
  13.     102: ErrorMsg := tr('File not assigned');
  14.     103: ErrorMsg := tr('File not open');
  15.     104: ErrorMsg := tr('File not open for input');
  16.     105: ErrorMsg := tr('File not open for output');
  17.   else
  18.     ErrorMsg := Format(tr('Error %d occurred'), [Err]);
  19.   end;
  20.   if Err <> 0 then
  21.   raise Exception.Create(ErrorMsg);
  22. end;
  23.  
  24. procedure OpenFileToWrite(var
  25.   FileHandle: TextFile;
  26.   AFileName: String;
  27.   AContent: String);
  28. begin
  29.   {$push}{$I-}
  30.   AssignFile(FileHandle, AFileName);
  31.   Rewrite(FileHandle);
  32.   CheckError(IOResult);
  33.  
  34.   WriteLn(FileHandle, AContent);
  35.   CloseFile(FileHandle);
  36.   {$pop}
  37. end;
  38.  
  39. procedure OpenFileToRead(var
  40.   FileHandle: TextFile;
  41.   AFileName: String; var
  42.   AContent: String);
  43. var
  44.   ALine: String;
  45. begin
  46.   System.Assign(FileHandle, AFileName);
  47.   {$push}{$I-}
  48.   filemode := 0;
  49.   Reset(FileHandle);
  50.   CheckError(IOResult);
  51.   {$pop}
  52.  
  53.   AContent := '';
  54.   while not Eof(FileHandle) do
  55.   begin
  56.     ReadLn(FileHandle, ALine);
  57.     AContent := AContent + ALine + sLineBreak;
  58.   end;
  59. end;

and in use:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.sbLoadFromFile1Click(Sender: TObject);
  2. var
  3.   TempFile: TextFile;
  4.   TempSrc: String;
  5.   TempName: String;
  6. begin
  7.   TempName := 'mytemp_' + IntToStr(Random(100000)) + '.tmp';
  8.   TempSrc  := CommentLexer(synEditHeaderContent.Lines.Text).Text;
  9.  
  10.   OpenFileToWrite(TempFile, TempName, TempSrc);
  11.   OpenFileToRead (TempFile, TempName, TempSrc);
  12.  
  13.   CloseFile(TempFile);
  14. end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

 

TinyPortal © 2005-2018