Sorry to bother everyone, I'm new to QB and need a little help. I have gotten to a point where I need to store data and I figure the easiest way to do so would be to write/read to a flat file. What I could find only worked partly. I am able to OPEN the file or create it if it doesn't exist by doing the following.
DIM fh AS STRING
fh = FREEFILE
OPEN "PLANTS.DAT" FOR RANDOM AS #fh
This seems to work, it will create the file if it doesn't exist. My problem comes in at trying to write to the file and read from it. I have been trying the following line that goes with the above example.
PRINT #fh, "This should be line 1"
but that doesn't seem to write anything to the file. Any idea's on this? I am using QuickBasic 4.5 running XP PRO and I don't have any problems with other QB programs running on XP so I don't think that's it.
While I have you. If you know how to read a line from a file then parse it, it would be helpful :P
In the above example I am wanting to write lines to the file that will look like:
100:PLANT1
200:PLANT2
and so on... When I read the information back I need to be able to read a line and parse it by the delimiter.. in this case : ... It can be a comma if it has to be, that doesn't really matter.
Thanks for the help in advance!
open/read/write to a flat file
Moderators:Administrator, Global Moderator
Re: open/read/write to a flat file
For a flat file, you need OUTPUT, not RANDOM.NuKMaN wrote:write/read to a flat file.
Mac
Code: Select all
CLS
DIM fh AS INTEGER
fh = FREEFILE
PRINT "Start writing"
OPEN "PLANTS.DAT" FOR OUTPUT AS #fh
PRINT #fh, "100:PLANT1"
PRINT #fh, "200:PLANT2"
CLOSE #fh
PRINT "Finished writing"
fh = FREEFILE
OPEN "PLANTS.DAT" FOR INPUT AS #fh
PRINT "Start reading"
DO WHILE NOT EOF(1)
LINE INPUT #1, l$
y = INSTR(l$, ":")
IF y = 0 THEN STOP' No delimiter
PRINT LEFT$(l$, y); " "; MID$(l$, y + 1)
LOOP
CLOSE #fh
PRINT "Finished reading"
SYSTEM
Your code starts as:
As soon as you try to run this, you should get an error message,
"Type mismatch"
with the cursor pointing to the variable in the second line, fh, because you first define it as a string, then try to make it equal FREEFILE, which is an integer.
Mac's code will do what you need, but, I just thught you should know the above.
Code: Select all
DIM fh AS STRING
fh = FREEFILE
"Type mismatch"
with the cursor pointing to the variable in the second line, fh, because you first define it as a string, then try to make it equal FREEFILE, which is an integer.
Mac's code will do what you need, but, I just thught you should know the above.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.
Good detail to point out, Ralph.Ralph wrote:Mac's code will do what you need, but, I just thught you should know the above.
As I read my post again, I found that I had a bug: I used eof(1) instead of required eof(fh). Also had a line input #1, l$. Just luck the program ran. Here is debugged:
Code: Select all
CLS
DIM fh AS INTEGER
fh = FREEFILE
PRINT "Start writing"
OPEN "PLANTS.DAT" FOR OUTPUT AS #fh
PRINT #fh, "100:PLANT1"
PRINT #fh, "200:PLANT2"
CLOSE #fh
PRINT "Finished writing"
fh = FREEFILE
OPEN "PLANTS.DAT" FOR INPUT AS #fh
PRINT : PRINT "Start reading"
DO WHILE NOT EOF(fh)
LINE INPUT #fh, l$
y = INSTR(l$, ":")
IF y = 0 THEN STOP' No delimiter
PRINT LEFT$(l$, y); " (Delimiter detected) "; MID$(l$, y + 1)
LOOP
CLOSE #fh
PRINT "Finished reading"
SYSTEM