reading random access files

New to FreeBASIC? Post your questions here.
Post Reply
dcouzin
Posts: 2
Joined: Dec 17, 2018 2:55
Location: Berlin, Germany

reading random access files

Post by dcouzin »

I wrote these three lines in QuickBASIC many years ago:
  • open "list"+left$(s$(si+4),4)+".csv" as #2 len=4
    field #2, 4 as pk$
    GET #2, 100
Don't worry about the first line. It's the next two lines that the FB compiler, even with option '-lang qb', rejects, giving these two reasons:
  • error 4: Duplicated definition, field
    error 16: Expected ','
So FB (even with option '-lang qb') does not read random access files the way QB did. How can I rewrite the two lines so the old program compiles?

Thanks.
DC
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: reading random access files

Post by Tourist Trap »

dcouzin wrote: field #2, 4 as pk$
Hello DC,

what I see first is that FIELD is a keyword so you must write #undef field to get rid of it. It's only needed with Unions:
Specifies field alignment.
Syntax
Type|Union typename Field = { 1 | 2 | 4 }
...
End Type|Union
Hope it helps?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: reading random access files

Post by fxm »

In FB, FIELD statement (for record definition at runtime) has been left aside. The keyword FIELD is used in FB to specify field alignment in Type variables.
To define fields in a file buffer, TYPE(s) must be used.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: reading random access files

Post by counting_pine »

The Field statement proved difficult to replicate in FB, so unfortunately it's not supported.
I'm not an expert in Field, but it looks like it just takes all 4 bytes of the record, and put it into pk$.

So something like this might work:

Code: Select all

open "list"+left$(s$(si+4),4)+".csv" as #2 len=4
pk$ = space$(4)
GET #2, 100, pk$
(I'm not that comfortable with Random mode though, so I'll post what is probably the equivalent in Binary mode):

Code: Select all

open "list"+left$(s$(si+4),4)+".csv" for binary as #2
pk$ = space$(4)
GET #2, 99*4+1, pk$
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: reading random access files

Post by Munair »

As most programming languages these days do not support random access files (fixed length records), I replicated this using a binary stream object several years ago in Real Basic as a means of providing a simple and fast database where fixed records are sufficient. I haven't got around to translating it to FB yet and I didn't feel the need as FB supports random access files natively. Perhaps I will do a port after all.
dcouzin
Posts: 2
Joined: Dec 17, 2018 2:55
Location: Berlin, Germany

Re: reading random access files

Post by dcouzin »

counting_pine's suggestion worked perfectly! Correct answers appear, and extremely fast.

I made much use of random access files in QB. In this case the records are single-precision (4-byte) floats and the file contains over 600,000 of them. For numerical approximation programs of this type, running on PCs of the 1990s, the random access file was necessary.

Since

Code: Select all

open "file.csv" as #2 len=4
pk$ = space$(4)
GET #2, 100, pk$

does in FB exactly what

Code: Select all

open "file.csv" as #2 len=4
field #2, 4 as pk$
GET #2, 100

did in QB, I hope this, with suitable generalization but understandable by QB people, can be added to the FB documentation.

Thanks much,
DC
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: reading random access files

Post by jj2007 »

fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: reading random access files

Post by fxm »

dcouzin wrote:I hope this, with suitable generalization but understandable by QB people, can be added to the FB documentation.
I think that for a string, it is well explained (description + examples) on the page of the documentation on 'GET # (File I/O)' that we must provide a string 's' well sized to the size 'N' desired (because 'amount' is not allowed):
- either by 'Dim As String * N s',
- either by 'Dim As W/Zstring * N+1 s',
- or by 'Dim As String s' and 's = String (N, 0)' for example.
If we provide 'Get #' with a dereferenced [W/Z]String pointer, as the compiler can not guess the desired 'N' number of elements, the referenced buffer must begin with 'N' nonzero elements.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: reading random access files

Post by counting_pine »

Yeah. I'm reluctant to encourage the use of fixed-length or null-terminated strings here though, because their lengths aren't particularly well-defined in this context.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: reading random access files

Post by fxm »

I simply detailed all the different syntactical possibilities with strings, without wearing judgment or advice.
Post Reply