Recent

Author Topic: Open a read-only untyped file for reading  (Read 687 times)

pelle

  • Newbie
  • Posts: 6
Open a read-only untyped file for reading
« on: November 17, 2024, 07:24:13 pm »
There is probably some obvious solution here that I overlook...

I have minimized my problem to this tiny program:

Program OpenFile;
uses SysUtils;
Var fin : File;
begin
  Assign(fin, ParamStr(1));
  Reset(fin);
end.


(Obviously my real program is also doing some reading from the file. I want to parse out some data from a binary file.)

Experimenting with this in FreeBSD with fpc 3.2.3. The final program will be compiled for 16-bit DOS, but I will worry about that part later. Have not tested this on any other system.

The above works fine as long as I try to use it on a file that exists and is writeable.

If I try it on a write-protected file however it fails with this error:
EInOutError: Access denied

If I read the documentation for Reset correctly this is kind of expected behavior, as the mode used for untyped files will be "using the mode specified in filemode" and that constant is set to 2 (i.e. read/write) (https://www.freepascal.org/docs-html/rtl/system/filemode.html)?

So, without re-compiling fpc to modify that constant... how do I open a read-only file for reading? Is there some procedure I am not finding that I should use instead of Reset if I just want to read the file? I only find Rewrite and Append, but those do the opposite of what I am trying to achieve?

(I can work around this easily since luckily I can make my files read-write, so no panic for now. I preferred to keep the files read-only to avoid accidentally modifying files, but since I have everything under version control anyway if I happen to modify a file I can revert it anyway. But I am still really curious what the correct way of doing this would be.  :))

Fibonacci

  • Hero Member
  • *****
  • Posts: 646
  • Internal Error Hunter
Re: Open a read-only untyped file for reading
« Reply #1 on: November 17, 2024, 07:29:07 pm »
Just set FileMode := 0 in your code

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: Open a read-only untyped file for reading
« Reply #2 on: November 17, 2024, 07:34:50 pm »
I have no issues setting the FIleMode ?

Also, you should specify the File Type ? File of Byte for example.

otherwise, all records are 128 bytes.
The only true wisdom is knowing you know nothing

pelle

  • Newbie
  • Posts: 6
Re: Open a read-only untyped file for reading
« Reply #3 on: November 17, 2024, 07:51:20 pm »
Just set FileMode := 0 in your code

That worked. Thanks!

Now I instead wonder why something is a constant but can still be modified?! Is a "const" in Pascal something very different from constants in other languages?

const FileMode: Byte = 2;

EDIT: Never mind. I found the answer myself at https://wiki.freepascal.org/Const Unexpected, but obviously how it is supposed to work.
« Last Edit: November 17, 2024, 07:54:13 pm by pelle »

pelle

  • Newbie
  • Posts: 6
Re: Open a read-only untyped file for reading
« Reply #4 on: November 17, 2024, 07:52:51 pm »
I have no issues setting the FIleMode ?

Also, you should specify the File Type ? File of Byte for example.

otherwise, all records are 128 bytes.

Thanks! I was not sure about this part, but I had actually tried also with file of byte and still had the same error, so I never got as far as actually reading bytes from the file.

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: Open a read-only untyped file for reading
« Reply #5 on: November 17, 2024, 07:57:09 pm »
My point is if you specify let's say 1 in the BlockRead or just read function, you will read in 128 bytes.

Those functions work on Block size and when you don't specify the block size in this case, it should be 128 if memory serves me.
The only true wisdom is knowing you know nothing

pelle

  • Newbie
  • Posts: 6
Re: Open a read-only untyped file for reading
« Reply #6 on: November 17, 2024, 08:13:08 pm »
My point is if you specify let's say 1 in the BlockRead or just read function, you will read in 128 bytes.

Those functions work on Block size and when you don't specify the block size in this case, it should be 128 if memory serves me.

Yes, that seems to be the case. I only want to read 1 byte at a time really, since I have to decode the file one byte at a time
(it is compressed with some simple RLE algorithm, but I got that part to work already... now on to parse the decompressed data!).

Everything is fine. Thanks everyone!

 

TinyPortal © 2005-2018