BBC BASIC for Windows
General >> General Board >> Reading/writing a simple text file.
http://bb4w.conforums.com/index.cgi?board=general&action=display&num=1272097165

Reading/writing a simple text file.
Post by Dyna on Apr 24th, 2010, 08:19am

Hi,
I have just moved over to BBC basic for windows because I no longer like VB basic, it's gone stupid. wink

I would like to know how to read/write a simple data text file, I can't find information on this anywhere.

In VB it would go something like this.
------------------------------------------------------
'LOAD FILE

Mydata = App.Path & "\Data\MyData.txt"

Open Mydata For Input As #1
Do While Not EOF(1)
Input #1, Name(a)
a = a + 1
Loop
Close #1

Count = a

'******SAVE DATA ******

Mydata = App.Path & "\Data\MyData.txt"

Open Mydata For Output As #1

For a = 0 To Count
Write #1, Name(a)
Next a

Close #1
-------------------------------------------

How would I go about writing this in BBC basic?
Thank you. smiley
Re: Reading/writing a simple text file.
Post by admin on Apr 24th, 2010, 10:29am

on Apr 24th, 2010, 08:19am, Dyna wrote:
How would I go about writing this in BBC basic?

Code:
      REM LOAD FILE
      
      Mydata$ = @dir$ + "Data\MyData.txt"
      
      infile = OPENIN(Mydata$)
      IF infile=0 THEN ERROR 53, "Cannot open file"
      
      WHILE (NOT EOF #infile)
        INPUT #infile, Name$(a)
        a = a + 1
      ENDWHILE
      CLOSE #infile
      
      Count = a
      
      REM ******SAVE DATA ******
      
      Mydata$ = @dir$ + "Data\MyData.txt"
      
      outfile = OPENOUT(Mydata$)
      IF outfile=0 THEN ERROR 53, "Cannot create file"
      
      FOR a = 0 TO Count
        PRINT #outfile, Name$(a)
      NEXT a
      
      CLOSE #outfile 

The file's records will be terminated by CR (CHR$13). If you want to read/write a CRLF-terminated file you need to add a little code thus:

Code:
        INPUT #infile, Name$(a)
        IF ASC(Name$(a))=10 THEN Name$(a) = MID$(Name$(a),2) 


Code:
        PRINT #outfile, Name$(a)
        BPUT #outfile, 10 

Richard.


Re: Reading/writing a simple text file.
Post by Dyna on Apr 24th, 2010, 3:32pm

Thank you very much Richard, I think that will do the trick nicely.
The file's records terminated by CR (CHR$13) thing is newish to me and will take a bit of playing with, but that should be no problem. smiley