User Tools

Site Tools


reading_20and_20writing_20plain_20text_20files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
reading_20and_20writing_20plain_20text_20files [2018/03/31 13:19] – external edit 127.0.0.1reading_20and_20writing_20plain_20text_20files [2024/01/05 00:21] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 //by Richard Russell, August 2007//\\ \\  Standard text files, such as are created by text editors like Windows **Notepad** or MS-DOS **EDIT**, consist of lines of plain text separated by the character sequence //Carriage Return, Line Feed// (usually abbreviated as **CRLF**). Plain text files are probably the most universal of all file formats, especially if you limit their contents to the 7-bit [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwinb.html#ascii|ASCII]] character set. They can be understood by very many programs, even if not their 'native' file format, and form the basis of many other structured file formats such as [[/Reading%20and%20writing%20CSV%20files|Comma Separated Value]] files.\\ \\  Although //BBC BASIC for Windows// does not have built-in support for CRLF-delimited text files it is very easy to read and write files in that format. Since the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#printhash|PRINT#]] statement delimits string values with //Carriage Return// (**CR**) all you need to do to create a standard text file is to add a //Line Feed// (**LF**) after each line:\\ \\  //by Richard Russell, August 2007//\\ \\  Standard text files, such as are created by text editors like Windows **Notepad** or MS-DOS **EDIT**, consist of lines of plain text separated by the character sequence //Carriage Return, Line Feed// (usually abbreviated as **CRLF**). Plain text files are probably the most universal of all file formats, especially if you limit their contents to the 7-bit [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwinb.html#ascii|ASCII]] character set. They can be understood by very many programs, even if not their 'native' file format, and form the basis of many other structured file formats such as [[/Reading%20and%20writing%20CSV%20files|Comma Separated Value]] files.\\ \\  Although //BBC BASIC for Windows// does not have built-in support for CRLF-delimited text files it is very easy to read and write files in that format. Since the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#printhash|PRINT#]] statement delimits string values with //Carriage Return// (**CR**) all you need to do to create a standard text file is to add a //Line Feed// (**LF**) after each line:\\ \\ 
 +<code bb4w>
         LF = 10         LF = 10
  
Line 16: Line 17:
  
         CLOSE #outfile%         CLOSE #outfile%
 +</code>
 Note that numeric values must be written as strings, hence the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#str|STR$]]. For maximum compatibility you should avoid incorporating control characters (i.e. characters with ASCII codes less than 32) other than the **CRLF** sequence, although often //Horizontal Tab// characters (**HT** or CHR$9) will be acceptable.\\ \\  Similarly, since the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin5.html#inputhash|INPUT#]] statement reads string records terminated by **CR**, all you need to do to read a standard text file is discard the extra **LF** character if present:\\ \\  Note that numeric values must be written as strings, hence the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#str|STR$]]. For maximum compatibility you should avoid incorporating control characters (i.e. characters with ASCII codes less than 32) other than the **CRLF** sequence, although often //Horizontal Tab// characters (**HT** or CHR$9) will be acceptable.\\ \\  Similarly, since the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin5.html#inputhash|INPUT#]] statement reads string records terminated by **CR**, all you need to do to read a standard text file is discard the extra **LF** character if present:\\ \\ 
 +<code bb4w>
         LF = 10         LF = 10
  
Line 32: Line 35:
  
         CLOSE #infile%         CLOSE #infile%
 +</code>
 Since the **LF** //follows// the terminating **CR** it will appear as the //first// character in each record (except the first record). Note that numeric values must be read as a string and converted back to a number, hence the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#val|VAL]].\\ \\  Line terminations other than **CRLF** are in use, for example Unix-based file systems generally use a single **LF** as the line termination; **CR** and **LFCR** are also occasionally encountered. If you want to read files in any of these formats you can use the 'universal' **FNreadline** function below:\\  Since the **LF** //follows// the terminating **CR** it will appear as the //first// character in each record (except the first record). Note that numeric values must be read as a string and converted back to a number, hence the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin7.html#val|VAL]].\\ \\  Line terminations other than **CRLF** are in use, for example Unix-based file systems generally use a single **LF** as the line termination; **CR** and **LFCR** are also occasionally encountered. If you want to read files in any of these formats you can use the 'universal' **FNreadline** function below:\\ 
 +<code bb4w>
         DEF FNreadline(F%)         DEF FNreadline(F%)
         LOCAL A$         LOCAL A$
Line 38: Line 43:
         IF A$="" IF PTR#F%>1 THEN PTR#F%=PTR#F%-2:IF BGET#F%<>BGET#F% A$=GET$#F%         IF A$="" IF PTR#F%>1 THEN PTR#F%=PTR#F%-2:IF BGET#F%<>BGET#F% A$=GET$#F%
         = A$         = A$
 +</code>
 This function will correctly return blank lines, when they are present in the file. If you're not interested in blank lines, and want to discard them, you can use this function instead:\\  This function will correctly return blank lines, when they are present in the file. If you're not interested in blank lines, and want to discard them, you can use this function instead:\\ 
 +<code bb4w>
         DEF FNreadlinenoblank(F%)         DEF FNreadlinenoblank(F%)
         LOCAL A$         LOCAL A$
Line 45: Line 52:
         UNTIL A$<>"" OR EOF#F%         UNTIL A$<>"" OR EOF#F%
         = A$         = A$
 +</code>
reading_20and_20writing_20plain_20text_20files.1522502376.txt.gz · Last modified: 2024/01/05 00:16 (external edit)