Page 1 of 1

open/read/write to a flat file

Posted: Thu Mar 06, 2008 2:24 pm
by NuKMaN
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!

Re: open/read/write to a flat file

Posted: Thu Mar 06, 2008 8:32 pm
by Mac
NuKMaN wrote:write/read to a flat file.
For a flat file, you need OUTPUT, not RANDOM.

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
Mac

Posted: Fri Mar 07, 2008 1:54 am
by Ralph
Your code starts as:

Code: Select all

DIM fh AS STRING 
fh = FREEFILE 
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.

Posted: Fri Mar 07, 2008 12:55 pm
by Mac
Ralph wrote:Mac's code will do what you need, but, I just thught you should know the above.
Good detail to point out, Ralph.

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
Mac