BBC BASIC for Windows
« How do I read text from a file? »

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



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: How do I read text from a file?  (Read 859 times)
anonymous2506
New Member
Image


member is offline

Avatar




PM


Posts: 1
xx How do I read text from a file?
« Thread started on: Mar 21st, 2016, 11:41pm »

Hello

I'm currently working on an encryption program that requires encrypted text to be read from a file, encrypted and then fully decrypted.

Having finished the encryption and decryption part, i am having trouble with actually obtaining the file - here is what i have done so far:

max%=100
DIM text$(max%)
index% = 0
file% = OPENINtitle$
IF file% = 0 ERROR 100, "Couldn't open "title$
REPEAT
READ 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%

When i enter a valid file name in (hello.txt) it comes up with the 'couldn't open' error message.

Thank you for any help in advance.

Anonymous2506
« Last Edit: Mar 21st, 2016, 11:43pm by anonymous2506 » User IP Logged

michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx Re: How do I read text from a file?
« Reply #1 on: Mar 22nd, 2016, 12:20am »

That's all I did was work with compression of data in files and extraction.

Here is my simplest example of data write and read.

This is really all a person needs to set up any data base or encryption.. The thing with encryption I could show you but I would be posting code that cant be here.

(when I translate it I will post an example, and that's pretty easy)

This is BBC Basic version... I'm only a week here so... I am trying to learn as I post..

Code:
 REM THE SIMPLEST FILE DATA MANAGEMENT EXAMPLE ON THIS FORUM !!
      REM OPEN A FILE FOR OUTPUT AND WRITE 4 DIFFRENT PIECES OF DATA (Even if it doesnt look like it)
      A=OPENOUT(@usr$+"JUNK.DAT")
      REM The next command line will write 3 strings and 1 number to the file*******
      REM VVVVV notice how A becomes an identifier or device handle FOR THE FILE YOU MAKE***
      PRINT#A,"REALLY?","THERE ARE",4,"DATA SECTIONS TO THIS DATA"
      CLOSE#A
      REM OPEN A FILE AND READ THE CONTENTS AND PRINT THEM ON THE SCREEN
      A=OPENIN(@usr$+"JUNK.DAT")
      REM VVVVV notice how A becomes an identifier or device handle FOR THE FILE YOU READ***
      INPUT#A,A$,B$,N,C$
      PRINT A$;" ";B$;" ";N;" ";C$
      REM ^^^^^^ The read must be the same as the written file for accuracy ^^^^^
      REM break down the elements of the data so they can see
      PRINT
      PRINT "1 ";A$
      PRINT "2 ";B$
      PRINT "3 (this is not a string) -";N
      PRINT "4 ";C$
      END
  
« Last Edit: Mar 22nd, 2016, 10:39am by michael » User IP Logged

I like making program generators and like reinventing the wheel
DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
xx Re: How do I read text from a file?
« Reply #2 on: Mar 22nd, 2016, 2:12pm »

Hi Anon,

I suspect that the file you are trying to open is not in the folder where BB4W is looking! If the program is saved and run from there, it will look in the same folder.

If you use
file%=OPENIN""

(or OPENIN"*.txt")

Then a file selection window will open, allowing you to browse to your file.

I note that your code snippet includes

READ file%,text$

... but to read from a file you should use READ# (or INPUT#):

READ#file%,text$

Hope that helps,

D
User IP Logged

KenDown
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 181
xx Re: How do I read text from a file?
« Reply #3 on: Apr 3rd, 2016, 04:14am »

I wonder if you would find the following bit of code useful?

REM ----- Find the data directory -----
atdir$=@dir$
F%=OPENIN(atdir$+"NewNames2.txt"):CLOSE#F%
IFF%=0THEN
dirletter%=67
REPEAT
atdir$=CHR$dirletter%+":\U3A\"
F%=OPENIN(atdir$+"NewNames2.txt"):CLOSE#F%
IFF%=0dirletter%+=1
UNTILF%>0ORdirletter%=79
IFdirletter%=79PROCwarn("Data file not found"):WAIT 500:QUIT
ENDIF

This starts off by looking in the same directory as the program (@dir$) but if it doesn't find the required file it begins at C:\ and checks if the file is on the C:\ drive. If not it looks in D:\ and so on down to O:\ (CHR$79) You can alter the 79 if you want to proceed beyond O!

Notice that the file I am looking for in this program will always be in a directory called "U3A". You will need to alter that if your file is in some other directory.


If you are using strings in a text file, I find that the easiest way of handling them is like this:

REM To write a string
BPUT#filehandle%,string$+CHR$13

REM To read a string
string$=GET$#filehandle%:IFstring$=""string$=get$#filehandle%

By adding CHR$13 you make your file easily readable by you - each item is on a separate line. However GET$# reads the CHR$13 as a separate, blank line. A trifle annoying but worth it for the convenience of having a human-readable file.
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