Qbasicnews.com

Full Version: Question regard random access file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi. my random access program is giving me a bad record length error at Put #3, x, all$. and i have no idea what im doing wrong.

code below

the objective of this program is to take some info from a sequencial file. put them into a random access file. make all name, age, city have the same length and put them all into one string. print name, age, city out by using the mid statement.

Code:
REM Variable Dictionary
REM file$.....file address that contains the data
REM out$......the random access file output
REM port#.....ports for both input and output
REM count.....counts how many sets of data there are
REM namelen...longest name length
REM agelen....longest age length
REM citylen...longest city length
REM totlen....total length of the whole string
REM all.......the string containing name, city and age
REM nameadd...number of blanks to be added
REM ageadd....ditto
REM cityadd...ditto

CLS

CONST file$ = "f:\qb\people.txt"
CONST out$ = "f:\qb\out.dat"
CONST port = 1
CONST port2 = 2

OPEN file$ FOR INPUT AS #port

count = 0

WHILE NOT EOF(port)
  INPUT #port, name$, age$, city$
  count = count + 1
  IF LEN(name$) > namelen THEN namelen = LEN(name$)

  IF LEN(city$) > citylen THEN citylen = LEN(city$)

WEND

CLOSE #port

agelen = 3

totlen = namelen + citylen + agelen

OPEN file$ FOR INPUT AS #port2
OPEN out$ FOR RANDOM AS #3 LEN = totlen

FOR x = 1 TO count
  INPUT #port2, name$, age$, city$
  nameadd = namelen - LEN(name$)
  FOR y = 1 TO nameadd
    name$ = name$ + " "
  NEXT y

  ageadd = agelen - LEN(age$)
  FOR y = 1 TO ageadd
    age$ = age$ + " "
  NEXT y

  cityadd = citylen - LEN(city$)
  FOR y = 1 TO cityadd
    city$ = city$ + " "
  NEXT y
  all$ = name$ + age$ + city$
  PUT #3, x, all$

NEXT x

CLOSE #3
CLOSE #port2


OPEN out$ FOR RANDOM AS #3 LEN = totlen


FOR x = 1 TO count
  GET #3, x, all$
  PRINT MID$(all$, 1, namelen), ;
  PRINT MID$(all$, namelen + 1, namelen + 1 + agelen), ;
  PRINT MID$(all$, namelen + agelen + 1, namelen + agelen + citylen + 1), ;
NEXT x

CLOSE #3

END
you're not working with the "RANDOM" file correctly...

Code:
CLS

TYPE record
    nm AS STRING * 20
    age AS INTEGER
    city AS STRING * 15
END TYPE

DIM rec AS record

CONST file$ = "e:\people.txt"
CONST out$ = "e:\out.dat"
CONST port = 1
CONST port2 = 2

OPEN file$ FOR INPUT AS #port

count = 0
ON ERROR GOTO errhand

DO
  INPUT #1, name$, age$, city$
  PRINT name$, age$, city$
  count = count + 1
LOOP UNTIL EOF(1)

CLOSE #port

count = 4

agelen = 3

totlen = namelen + citylen + agelen

OPEN file$ FOR INPUT AS #port2
OPEN out$ FOR RANDOM AS #3 LEN = LEN(rec)

FOR x = 1 TO count
  INPUT #port2, name$, age$, city$

  rec.nm = name$
  rec.age = VAL(age$)
  rec.city = city$

  PUT #3, x, rec
NEXT x

CLOSE #3
CLOSE #port2


OPEN out$ FOR RANDOM AS #3 LEN = LEN(rec)

FOR x = 1 TO count
  GET #3, x, rec
  PRINT rec.nm, rec.age, rec.city
NEXT x

CLOSE #3

END

errhand:
PRINT "Error no "; ERR; "encountered"
PRINT "resuming..."
RESUME NEXT
Quote:you're not working with the "RANDOM" file correctly...

could u elaborate on that? i like to know what im doing wrong
did you see my corrected code? Basically the "RANDOM" file mode is used to store fixed length records. Thats exactly what the "TYPE" at the begining of my program does. It makes a fixed length record whose contents are replaced by taking inputs from the SEQUENTIAL file (i.e. people.txt) and then the record is dumped at once into the out.dat file. Checkout qb's docs on RANDOM. They are quite comprehensive.
im making them all the same length here:

Code:
FOR x = 1 TO count
  INPUT #port2, name$, age$, city$
  nameadd = namelen - LEN(name$)
  FOR y = 1 TO nameadd
    name$ = name$ + " "
  NEXT y

  ageadd = agelen - LEN(age$)
  FOR y = 1 TO ageadd
    age$ = age$ + " "
  NEXT y

  cityadd = citylen - LEN(city$)
  FOR y = 1 TO cityadd
    city$ = city$ + " "
  NEXT y
  all$ = name$ + age$ + city$
  PUT #3, x, all$

NEXT x

i add empty spaces to make sure that they are all the same length. then put them all into 1 string and store them in .dat
Akirika:

In your line:
OPEN out$ FOR RANDOM AS #3 LEN = totlen
you set LEN = totlen
But, below, you add a space to each variable, then try to fit the new record, which is now three spaces longer then totlen, into the random file's records, but, all$ is three bytes too long!!!

So, you must reestablish the length of totlen as = totlen+3, before you use the OPEN statement.

Does this explain why you are getting that error? Let us know.