A simple demo to show how to read from one file and write to another in BBC BASIC for Windows.  In this case, the program also demonstrates the use of a single user-defined function.
In pseudocode the program works as follows:
  open file for reading (input)
  open file for writing (ouput)
  while not at the end of the file:
      read next word in the input file
      reverse the word
      write the reversed word into the output file
  close both files
I tested this program using a dictionary file of over 100000 English words and the output can be seen below:
Full source code follows:
     REM T Street
     REM 2014-12-12
     REM 
reads each word from a file, reverses it and
     REM then dumps it back into 
a new file
     ON ERROR ERROR 0, "Oh dear, something went wrong "+REPORT$
     REM filenames
  
   Filename_input$ = "<put filename 
here>.txt" : REM change this
    
 Filename_output$ = "reversed.txt"
    
 REM carriage return line feed
     CRLF$ = CHR$(13)+CHR$(10)
    
 REM open up the file for reading
     FileIn% = 
OPENUP( Filename_input$ )
     FileOut% = OPENOUT( Filename_output$ )
     REM some helpful comments
     PRINT 
"Reading from " Filename_input$
     PRINT "Reversing 
words..."
     REM the M A I N  L O O P
    
 REM ---------------------
     WHILE 
NOT(EOF#FileIn%)
       REM read in each word
       INPUT#FileIn%, word$
       REM 
reverse it
       reversedWord$ = FN_reverseMe( word$ )
       REM 
write to new file
       PRINT#FileOut%, 
reversedWord$+CRLF$
     ENDWHILE
    
 REM now finished with every word
     REM so 
close both the files
     CLOSE#FileIn%
     CLOSE#FileOut%
     REM finish 
up
     PRINT "Done - 
saved to " Filename_output$
     STOP
     DEFFN_reverseMe( aString$ )
  
   REM reverses the string passed as parameter
     REM 
and returns it
     LOCAL i%
     LOCAL myNewString$
     REM step 
backwards through the string
     REM look at each character in sequence
  
   FOR i% = LEN(aString$) TO 1 STEP -1
       REM add on the 
current character
       myNewString$ += MID$( 
aString$, i%, 1)
     NEXT
     = 
myNewString$
Subscribe to:
Post Comments (Atom)
 
 
