reading random access files

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

reading random access files

Postby dcouzin » Jan 11, 2019 20:59

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: 2762
Joined: Jun 02, 2015 16:24

Re: reading random access files

Postby Tourist Trap » Jan 12, 2019 14:58

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
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: reading random access files

Postby fxm » Jan 12, 2019 15:00

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: 6172
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: reading random access files

Postby counting_pine » Jan 12, 2019 15:21

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: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: reading random access files

Postby Munair » Jan 12, 2019 16:24

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

Postby dcouzin » Jan 12, 2019 21:01

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: 1234
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: reading random access files

Postby jj2007 » Jan 13, 2019 3:11

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

Re: reading random access files

Postby fxm » Jan 14, 2019 10:19

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: 6172
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: reading random access files

Postby counting_pine » Jan 14, 2019 14:16

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
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: reading random access files

Postby fxm » Jan 14, 2019 14:32

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

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 22 guests