Tutorial 016 -Writing and Reading files using BBC Basic for Windows (BB4W), "Capitalisation" - ASCII codes

Prompted by discussions with  Paul Norton and Edward Whelan (to whom many thanks), the following BB4W program  illustrates that we can in principle :
In this example we write a text file (.txt file), (which can be read using Notepad, Word etc.,) and then read it back into BB4W and print it to the screen.

Here, to show the possibilities, we output the file as a single string but read it back an 8-bit byte at a time :

      REM: Lets write a text file using BB4W...
      REM: then read the text file back in again...
      MODE 8
      A$="BB4Wtext001.txt"
      X=OPENOUT(A$)
      PRINT"X, the output channel = ";X
      PRINT
      READ text$
      PRINT text$
      PRINT
      PRINT#X, text$
      BPUT#X,10 : REM : (10 is the ASCII value for a line feed)
      CLOSE#X
      :
      DATA " We have sent/received this text from a DATA line in a BB4W program"
      :
      PRINT"We have created a text file named BB4Wtext002.txt"
      PRINT"....in the same directory in which this program is stored"'
      PRINT"Now just press a key to read it back in!"'
      G=GET
      VDU14
      file$=A$
      fnum=OPENIN file$
      PRINT"fnum, the input channel = ";fnum
      PRINT
      IF fnum=0 THEN PRINT "No ";file$;" data": END
      REPEAT
        temp=BGET#fnum : REM Read byte
        PRINT CHR$(temp);
      UNTIL  EOF#fnum
      CLOSE#fnum


Annotated Listing

      REM: Lets write a text file using BB4W...
      REM: then read the text file back in again...

      MODE 8  *** I usually use this mode  then colour is availble if you want to add it later ***

      A$="BB4Wtext002.txt" *** This is the title of the text file as it will be saved ***

      X=OPENOUT(A$)  *** Here we instigate the formation of a file ***

      PRINT"X, the output channel's number = ";X   *** The channel is a special memory location and is assigned a small number ***

      PRINT *** inserts a blank line to look prettier ***

      READ text$ *** reads the string stored in the DATA line ***

      PRINT text$ *** lets have a look at it ****

      PRINT

      PRINT#X, text$  *** This line places the data, in this case a string, into the output channel (memory) #X***

       BPUT#X, 10 : REM : (10 is the ASC code for a line feed) *** This avoids the cursor overwriting the start of the last line printed.

      CLOSE#X *** This closes off the file with a special character and causes the file to be written to disc (hard or soft!).***
      :
      DATA " We have sent/received this text from a DATA line in a BB4W program"
      :
      PRINT"We have created a text file named BB4Wtext002.txt"
      PRINT"....in the same directory in which this program is stored"'

      PRINT"Now just press a key to read it back in!"'
      G=GET

      VDU14  *** switches paged mode on in case you have a lot of output and the screen would scroll ***

      file$=A$  *** The rest of the program was written first as a separate one so I have left it as it was ***

      fnum=OPENIN file$ *** fnum is the channel (memory) into which the data from the file will be read ***

      PRINT"fnum, the input channel's number = ";fnum  *** Interesting to see what channel has been assigned ***

      PRINT

      IF fnum=0 THEN PRINT "No ";file$;" data": END  *** detects if the file does not exist ***

      REPEAT

        temp=BGET#fnum : REM: Read byte *** Its an eight-bit bite ***

        PRINT CHR$(temp);
*** temp contains the ASCII code for the character - see next program; CHR$ decodes it  and  each one is printed hard against the previous one by the "sticky" semicolon ***

      UNTIL  EOF#fnum  *** EOF#fnum returns the value TRUE (-1) when the end of the file is detected ***

      CLOSE#fnum *** Always remember to close the file when you've finished!



The ASCII code was introduced above - here are all the printable characters.  See the table of ASCII characters in the BB4W Help File for the non-printing so called control codes which are used to format text. (e.g. - VDU7 has the same effect as PRINT CHR$(7) in this case to sound a bleep)

      REM: Shows how the ASCII code works
      MODE8
      VDU14
      FOR n= 32 TO 126
        PRINT"CHR$(";n;") prints as ";CHR$(n)
        IF n=32 THEN PRINT"  That's a SPACE"
      NEXT n
      VDU7

Small characters to CAPITALS

illustrates how the ASCII code can be applied.

      MODE8
      name$="Richard Weston"
      PRINTname$
      PRINT'"Press a key to capitalise this :"'
      G=GET
      FOR n=1 TO LEN(name$)
        s$=MID$(name$,n,1)
        a=ASC(s$)
        IF a>96 AND a<123 THEN
          a=a-32
          s$=CHR$(a)
        ENDIF
        PRINTs$;
      NEXT
      PRINT


Annotated listing :

      MODE8
      name$="Richard Weston"
      PRINTname$
      PRINT'"Press a key to capitalise this :"'
      G=GET
      FOR n=1 TO LEN(name$) *** steps long the string "Richard Weston" - 14 characters including the space ***
        s$=MID$(name$,n,1) *** isolates the nth character ***

        a=ASC(s$) *** returns the ASCII code for each letter - also see the table of ASCII characters in the BB4W Help File
        IF a>96 AND a<123 THEN  *** small characters abcdef....xyz  ***
          a=a-32    *** converts the small letter to the corresponding capital letter ABCDE......XYZ ***
          s$=CHR$(a)
        ENDIF
        PRINTs$;
      NEXT
      PRINT




Next Tutorial

Richard Weston's Homepage