open/read/write to a flat file

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
NuKMaN
Newbie
Posts:7
Joined:Thu Mar 06, 2008 2:11 pm
open/read/write to a flat file

Post by NuKMaN » Thu Mar 06, 2008 2:24 pm

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!

Mac
Full Member
Posts:48
Joined:Wed Jun 29, 2005 2:01 am
Contact:

Re: open/read/write to a flat file

Post by Mac » Thu Mar 06, 2008 8:32 pm

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

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Fri Mar 07, 2008 1:54 am

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.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Mac
Full Member
Posts:48
Joined:Wed Jun 29, 2005 2:01 am
Contact:

Post by Mac » Fri Mar 07, 2008 12:55 pm

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

Post Reply