BBC BASIC for Windows
« Reading/writing a simple text file. »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:49pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Reading/writing a simple text file.  (Read 1008 times)
Dyna
New Member
Image


member is offline

Avatar




PM


Posts: 3
xx Reading/writing a simple text file.
« Thread started 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Reading/writing a simple text file.
« Reply #1 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.

« Last Edit: Apr 24th, 2010, 10:35am by admin » User IP Logged

Dyna
New Member
Image


member is offline

Avatar




PM


Posts: 3
xx Re: Reading/writing a simple text file.
« Reply #2 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
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls