Text files · 文本文件
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A file keeps data after the program stops
- The 2026–2028 syllabus (section 8.3) stores data in a text file.
- You open the file, read or write one item, then close it.
- This course keeps the file in memory for the one run, so you can see the result.
中文
文件在程序结束后仍保存数据
- 2026–2028 考纲(第 8.3 节)把数据存在文本文件里。
- 你先打开文件,读或写一项,然后关闭它。
- 本课程把文件保存在这一次运行的内存里,这样你能看到结果。
DECLARE LineOfText : STRING
OPENFILE "Note.txt" FOR WRITE
WRITEFILE "Note.txt", "Hello"
CLOSEFILE "Note.txt"
OPENFILE "Note.txt" FOR READ
READFILE "Note.txt", LineOfText
CLOSEFILE "Note.txt"
OUTPUT LineOfText
English
READ and WRITE
OPENFILE "name.txt" FOR READreads what is already there.OPENFILE "name.txt" FOR WRITEstarts a new file. Anything already in it is lost.READFILEputs the next item into a variable.WRITEFILEstores a value.CLOSEFILEwhen you have finished. A file is open in only one mode at a time.
中文
READ 与 WRITE
OPENFILE "name.txt" FOR READ读取已经存在的内容。OPENFILE "name.txt" FOR WRITE开始一个新文件。里面原有的内容会丢失。READFILE把下一项放进变量。WRITEFILE存入一个值。- 用完后
CLOSEFILE。一个文件一次只能以一种模式打开。
DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR WRITE
WRITEFILE "FileA.txt", "Saved"
CLOSEFILE "FileA.txt"
OPENFILE "FileA.txt" FOR READ
READFILE "FileA.txt", LineOfText
CLOSEFILE "FileA.txt"
OPENFILE "FileB.txt" FOR WRITE
WRITEFILE "FileB.txt", LineOfText
CLOSEFILE "FileB.txt"
OUTPUT LineOfText
English
Read every line
EOF("List.txt")isTRUEwhen there is no further line to read.WHILE NOT EOF(...)reads the whole file. EachREADFILEputs the next line into aSTRING.- Close the file when you finish. A later open can use it again.
中文
读出每一行
- 没有下一行可读时,
EOF("List.txt")为TRUE。 WHILE NOT EOF(...)读出整个文件。每次READFILE把下一行放进一个STRING。- 读完就关闭。之后还可以再次打开它。
DECLARE LineOfText : STRING
OPENFILE "List.txt" FOR WRITE
WRITEFILE "List.txt", "Red"
WRITEFILE "List.txt", "Blue"
CLOSEFILE "List.txt"
OPENFILE "List.txt" FOR READ
WHILE NOT EOF("List.txt") DO
READFILE "List.txt", LineOfText
OUTPUT LineOfText
ENDWHILE
CLOSEFILE "List.txt"
English
Watch out
- Declare the variable before you
READFILEinto it. WRITEreplaces the file. Read it first if you still need the old text.
中文
注意
- 在
READFILE读入之前,先声明变量。 WRITE会替换整个文件。如果还需要旧内容,先读出来。
English
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
中文
常见错误
- 先以
READ或WRITE打开,再READFILE或WRITEFILE,最后CLOSEFILE。 - 文件名是字符串,放在双引号里。
English
Now you try
- Write a line, read it back, and
OUTPUTit. - Press Check answer to test it.
中文
现在你来试
- 写入一行,再读回来,然后
OUTPUT它。 - 按检查答案来测试。
Write the line Hello to · 到 Note.txt, read it back into LineOfText, and output it. · 把一行 Hello 写入 Note.txt,再读回 LineOfText,并输出它。
Click Run to see the output here. · 点击“运行”查看此处输出。
Store Saved in · 入 FileA.txt, read it into LineOfText, write that same text to FileB.txt, and output it. · 把 Saved 存入 FileA.txt,读入 LineOfText,再把同一段文字写入 FileB.txt,并输出它。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write Cat and Dog to · 到 List.txt, then output both lines with WHILE NOT EOF. · 把 Cat 和 Dog 写入 List.txt,再用 WHILE NOT EOF 把两行都输出。
Click Run to see the output here. · 点击“运行”查看此处输出。