You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1593,7 +1600,7 @@ RECT 0,0,50,50 ' draw a rectangle
1593
1600
To specify “world” coordinates for the screen use the command `WINDOW(x1,x2,y2,y1)`. `WINDOW`
1594
1601
allows you to redefine the corners of the display screen as a pair of “world” coordinates. The
1595
1602
coordinates of the upper-left corner of the screen is given by [x1, y1], the lower-left corner
1596
-
by [x2, y2].The world space defined by WINDOW is disabled by a WINDOW command without parameters.
1603
+
by [x2, y2].The world space defined by WINDOW is disabled by a WINDOW command without parameters.
1597
1604
1598
1605
`VIEW(x1,y1,x2,y2)` defines a viewport with starting point (upper left corner)) [x1,y1] and end
1599
1606
point (lower right corner) [x2,y2]. Drawing outside the viewport is not possible.
@@ -1615,6 +1622,132 @@ w.setSize(800, 680) ' Set window size
1615
1622
1616
1623
For more information about graphics commands please see [Graphics](/pages/graphics.html).
1617
1624
1625
+
## Basic File Read and Write {#BasicFileReadAndWrite}
1626
+
1627
+
SmallBASIC supports various commands to read and write a file. Best suited for beginners are `PRINT` and `INPUT`. `PRINT` writes to a file in the same way as described in the section "Print on Screen". `INPUT` without additional parameters reads one line from a file. To read or write a file, the file needs first to be opened. This is done using the command `OPEN`:
1628
+
1629
+
```smallbasic
1630
+
OPEN file for INPUT|OUTPUT|APPEND as #1
1631
+
```
1632
+
1633
+
Using the command `OPEN`, a file with the file name `file` can be opened for reading (`INPUT`), for writing (`OUTPUT`), and writing to an existing file (`APPEND`). If you use the parameter `OUTPUT` and the file already exist, the file will be deleted. `#1` is a ID number you can chose freely, but it must start with `#`
1634
+
1635
+
To write to a file, `PRINT` can be used:
1636
+
1637
+
```smallbasic
1638
+
PRINT #1, "Hello world"
1639
+
```
1640
+
1641
+
`PRINT` has the same syntax as the print command in section "Print on Screen". To tell `PRINT`, that the output should be written to a file, use the file ID number as the first parameter.
1642
+
1643
+
With `INPUT`, data can be read from a file:
1644
+
1645
+
```smallbasic
1646
+
INPUT #1, s
1647
+
```
1648
+
1649
+
`INPUT` without additional parameters reads a line from file with the file ID `#1` into the variable `s`.
1650
+
1651
+
```smallbasic
1652
+
IF EOF(1) THEN PRINT "End of file reached"
1653
+
```
1654
+
1655
+
`EOF(FileID)` returns `1` if the end of the file is reached and no more data can be read from file. Please note, that in case of `EOF` the File ID is given without `#`.
1656
+
1657
+
To close a file, use `CLOSE #1`.
1658
+
1659
+
The following example writes the numbers 1 to 10 to a file and reads these numbers from a file and prints them on screen.
1660
+
1661
+
```smallbasic
1662
+
' create a text file
1663
+
OPEN "MyDemoFile.txt" FOR OUTPUT AS #1
1664
+
1665
+
FOR i = 1 TO 10
1666
+
PRINT #1, i
1667
+
NEXT
1668
+
1669
+
CLOSE #1
1670
+
1671
+
' open text file and print content line by line
1672
+
OPEN "MyDemoFile.txt" FOR INPUT AS #1
1673
+
1674
+
WHILE(!EOF(1)) ' eof works only without #
1675
+
INPUT #1, c
1676
+
PRINT c
1677
+
WEND
1678
+
1679
+
CLOSE #1
1680
+
```
1681
+
1682
+
### Save Arrays and Maps to a File {#SaveArraysAndMapsToAFile}
1683
+
1684
+
`TLOAD` and `TSAVE` provide a simple way to store arrays, maps or strings in a file and read them. Using a map, json data can be created and written to a file.
1685
+
1686
+
```smallbasic
1687
+
' Create an array with some data
1688
+
A << 1
1689
+
A << "test"
1690
+
A << 2
1691
+
PRINT A ' Output: [1,test,2]
1692
+
1693
+
' Save the array. This will create the file myfile.txt in
1694
+
' the same directory as your BASIC file
1695
+
TSAVE "myfile.txt", A
1696
+
1697
+
' Load the file
1698
+
TLOAD "myfile.txt", B
1699
+
PRINT B ' Output: [1,test,2,]
1700
+
```
1701
+
1702
+
### Save Variables to a File {#SaveVariablesToAFile}
1703
+
1704
+
With `READ` and `WRITE` numbers, strings and arrays can be read and written. The resulting file is a binary file. Viewing this file in a text editor will not show you anymore the context in a human readable form.
1705
+
1706
+
```smallbasic
1707
+
a = 12.3
1708
+
b = "test"
1709
+
c = [1,2,3,4]
1710
+
1711
+
' Write variables to file
1712
+
OPEN "text.bin" FOR OUTPUT AS #1
1713
+
WRITE #1, a, b, c
1714
+
CLOSE #1
1715
+
1716
+
' Read variables from file
1717
+
OPEN "text.bin" FOR INPUT AS #1
1718
+
READ #1, a, b, c
1719
+
CLOSE #1
1720
+
1721
+
PRINT a
1722
+
PRINT b
1723
+
PRINT c
1724
+
```
1725
+
1726
+
### Binary Files {#BinaryFiles}
1727
+
1728
+
`BGETC` and `BPUTC` can be used to read and write a byte from/to a binary file.
1729
+
1730
+
```smallbasic
1731
+
' create a binary file
1732
+
OPEN "BinaryFile.txt" for output as #1
1733
+
1734
+
FOR c = 0 TO 255
1735
+
BPUTC #1, c
1736
+
NEXT
1737
+
1738
+
CLOSE #1
1739
+
1740
+
' open binary file and print content
1741
+
OPEN "BinaryFile.txt" FOR INPUT AS #1
1742
+
1743
+
FOR i = 0 TO 255
1744
+
c = BGETC(1)
1745
+
PRINT CHR(c);
1746
+
NEXT
1747
+
1748
+
CLOSE #1
1749
+
```
1750
+
1618
1751
## OPTION {#Statement1}
1619
1752
1620
1753
The `OPTION` command is used to pass parameters to the SB-environment. There are
0 commit comments