BBC BASIC for Windows
« Opening a txt file »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:44pm



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: Opening a txt file  (Read 1188 times)
Danny
Guest
xx Opening a txt file
« Thread started on: Nov 2nd, 2009, 2:48pm »

Hello
Could someone let me know please how to open a .txt file using bb4w and then to read the contents into variables so that I can use the content for a simple language learning game.

I tend not to be able to understand anything very technical so would appreciate if someone could literally let me know what code would be required to open a file called spanish.txt

User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Opening a txt file
« Reply #1 on: Nov 2nd, 2009, 3:54pm »

Quote:
Could someone let me know please how to open a .txt file using bb4w and then to read the contents into variables

You don't say whether you want to use separate variables or an array. Assuming you will use an array (and that you know the maximum number of items you'll want to read) the code would be something like this:

Code:
      DIM text$(max%)
      index% = 0
      file% = OPENIN(@dir$+"spanish.txt")
      IF file% = 0 ERROR 100, "Couldn't open spanish.txt"
      REPEAT
        INPUT #file%, text$
        IF ASC(text$) = 10 text$ = MID$(text$,2)
        index% += 1
        text$(index%) = text$
      UNTIL EOF#file% OR index% = max%
      CLOSE #file%
      total% = index%
 

Richard.
User IP Logged

Danny H
Guest
xx Re: Opening a txt file
« Reply #2 on: Nov 3rd, 2009, 08:32am »

Thanks Richard

I am not able to get this working yet. I have given the variable max% a value of 100. I also changed the variable name at the bottom total% to ttal% as it gave a syntax error. I inserted a print statement to try and see the result on screeen. But it doesn't show any result. I would appreciate any assistance. Thanks, Danny.

max%=100

dim text$(max%)
index% = 0
file% = openin(@dir$+"spanish.txt")
if file% = 0 error 100, "Couldn't open spanish.txt"
repeat
input #file%, text$
if asc(text$) = 10 text$ = mid$(text$,2)
index% += 1
text$(index%) = text$

print text$

until eof#file% or index% = max%
close #file%
ttal% = index%
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Opening a txt file
« Reply #3 on: Nov 3rd, 2009, 09:05am »

Quote:
I also changed the variable name at the bottom total% to ttal% as it gave a syntax error

That means you're using the 'lowercase keywords' option. Because I don't use that option myself, I often accidentally write code (as in this case) which isn't compatible with it. Traditionally, BBC BASIC has always used CAPITAL keywords.

Apart from that, I've tested the program I listed: it works absolutely fine here. Without more detailed information I can't suggest what might be wrong, but BBC BASIC is easy to debug, so I'm sure it won't take you long to find out. See the section of the manual on debugging here:

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin2.html#debugging

I presume you've checked that your 'spanish.txt' file actually contains what you think it contains (e.g. by viewing it with Notepad)?

Richard.
User IP Logged

Danny H
Guest
xx Re: Opening a txt file
« Reply #4 on: Nov 3rd, 2009, 09:20am »

It works now thanks.
I ammended the text file, and as if by magic it worked.
User IP Logged

Ken Down
Guest
xx Re: Opening a txt file
« Reply #5 on: Apr 27th, 2010, 8:59pm »

I must admit that when reading a file prepared with NotePad (or other text editor) I prefer the GET$# command.

A line in NotePad ends with CHR$10+CHR$13 and GET$# will get everything up to the CHR$10, which means that the next GET$# command will read just the CHR$13, hence the following:

a$=GET$#F%:IFa$=""a$=GET$#F%

If you want to write to a text file, however, BBC BASIC only writes the string plus CHR$10. This means that all the lines run together in NotePad with funny rectangles between them. The solution is to add the CHR$13:

BPUT#F%,a$+CHR$13

The text file will now be readable in NotePad.
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Opening a txt file
« Reply #6 on: Apr 27th, 2010, 9:53pm »

on Apr 27th, 2010, 8:59pm, Guest-Ken Down wrote:
A line in NotePad ends with CHR$10+CHR$13

No it doesn't! A line in NotePad ends with CHR$13+CHR$10 (CRLF).

Richard.
« Last Edit: Apr 27th, 2010, 10:55pm by admin » 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