BBC BASIC for Windows
« Opening a txt file »

Welcome Guest. Please Login or Register.
Nov 21st, 2009, 09:24am




Pages: 1  Reply Notify Send Topic Print
 thread  Author  Topic: Opening a txt file  (Read 71 times)
Danny
Guest
xx Opening a txt file
« Thread started on: Nov 2nd, 2009, 3:48pm »
Quote

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

Richard Russell
Administrator
ImageImageImageImageImage

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 276
xx Re: Opening a txt file
« Reply #1 on: Nov 2nd, 2009, 4:54pm »
Quote

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, 09:32am »
Quote

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

Richard Russell
Administrator
ImageImageImageImageImage

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 276
xx Re: Opening a txt file
« Reply #3 on: Nov 3rd, 2009, 10:05am »
Quote

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, 10:20am »
Quote

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

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


New Monthly Ad-Free Plan!

$6.99 Gets 50,000 Ad-Free Pageviews!
| Free Shoutboxes | Hookah |

This Board Hosted For FREE By Conforums ©
Get Your Own Free Message Board!