random access files...

New to FreeBASIC? Post your questions here.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

random access files...

Postby thesanman112 » May 17, 2006 1:24

does anyone know why random access files doesnt seem to work properly,,, here is whats not workin...
open"test" for random as #1
open testout" for random as #2
' now suppose there are random numbers in file#1

for a=1 to 100
get #1,a,b$
put #2,a,b$
next

after i run this code, the bytes in file #1 dont get read at all...
Last edited by thesanman112 on May 17, 2006 1:34, edited 1 time in total.
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Re: random access files...

Postby blahboybang » May 17, 2006 1:29

thesanman112 wrote:for a=1 to 100
get #1,a,b$
pt #2,a,b$
next


It might have something to do with you using "pt #2,a,b$" instead of "put #2,a,b$"
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

no, that was just a typeing error in here...

Postby thesanman112 » May 17, 2006 1:33

any one can help would be appreciated...
D.J.Peters
Posts: 8189
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » May 17, 2006 3:17

hello thesanman112
sorry if i wrong but i think random access is for datas that are all in chunks of same size.

Do you use strings like that ?

dim b as string * 100 ' any fixed size

Joshy
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Postby Antoni » May 17, 2006 5:15

A file opened for random must have fixed length records, the record length must be specified when opening it. A random file has its records chain de without separators so the length you specify is the only way it has to know where a record ends.

The vars used for get and put must have the proper size. You should declare them as fixed length or fill them to the proper size before using them in get or put.

Code: Select all

open "test.dat" for random as #1 len=8
open "testout.dat" for random as #1 len=8

dim b$
b$=space$(8)
while not eof(1) 
get #1,,b$   
put#2,,b$
wend
close


You don't need to use the index in get and put if you read/write the file sequentially, as the pointer increases automatically.




for

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests