Posts: 1
Threads: 1
Joined: Dec 2004
Can anyone at all help me get qbasic's open command working? Every time I try to get it to work, it says "type mismatch".
code:
OPEN HIGHSCORES.DAT FOR RANDOM AS #1
p.s. How can I show the stuff in the file on screen, like a high score list.
Thanx
Posts: 2,765
Threads: 138
Joined: Nov 2002
Code:
OPEN "HIGHSCORES.DAT" FOR RANDOM AS #1
(you forgot the quotes)
also I usually use INPUT, OUTPUT, or BINARY (not RANDOM)
Code:
OPEN "name.dat" FOR INPUT AS #1
INPUT #1, name$
PRINT "Hello" ; name$
Quote:Ix
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 480
Threads: 24
Joined: Mar 2003
basic file access examples, off the top of my head:
input:
OPEN "blah.txt" FOR INPUT AS #1
LINE INPUT #1, lineoftext1$
LINE INPUT #1, lineoftext2$
CLOSE #1
print "First line of file: "; lineoftext1$
print "Second line of file: "; lineoftext2$
output:
OPEN "blah.txt" FOR OUTPUT AS #1
PRINT #1, "This is the first line I'm printing to the file."
PRINT #1, "This is the second line I'm printing to the file."
CLOSE #1
append:
OPEN "blah.txt" FOR APPEND AS #1
PRINT #1, "OOPS! Forgot this third line. I'll just add it without altering the first two!"
CLOSE #1
binary:
DIM onecharacter AS STRING * 1
FileByte% = 1
OPEN "blah.txt" FOR BINARY AS #1
get #1, FileByte%, onecharacter
print "The character at byte"; FileByte%; "is a "; onecharacter
onecharacter = "M"
put #1, FileByte%, onecharacter
print "Now it's an M!"
close #1
random:
TYPE PersonType
Name AS STRING * 20
Age AS INTEGER
FavoriteFood AS STRING * 30
END TYPE
DIM Person AS PersonType
OPEN "Blah.DAT" for RANDOM AS #1 LEN = LEN(Person)
Person.Name = "Bob Smith"
Person.Age = 45
Person.FavoriteFood = "Pizza"
PUT #1, 1, Person
print "Now Bob Smith, Age 45, is in record slot 1 in blah.dat"
Person.Age = 10
PUT #1, 2, Person
print "Now Bob Smith, Age 10, is in record slot 2 in blah.dat"
GET #1, 1, Person
print "Now Person.Age equals 45 years because we pulled from slot #1"
CLOSE #1
I hope I didn't make any mistakes there.
*peace*
Meg.
Posts: 6,419
Threads: 74
Joined: Mar 2002
The RANDOM mode is there for backwards compatibility. In QB 4.0 M$ introducted the BINARY mode as an intelligent replacement. BINARY is far more easy to deal with than RANDOM and allows variable length data to be read or written to a file.