Text files · ไฟล์ข้อความ (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 เป็นเวอร์ชันล่าสุด
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.
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 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.- ใช้
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.
ไทย
Read every line
EOF("List.txt")จะมีค่าเป็นTRUEเมื่อไม่มีบรรทัดถัดไปให้อ่านแล้ว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.
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.
ไทย
ระวัง
- Declare the variable before you
READFILEinto it. WRITEreplaces the file. Read it first if you still need the old text.
English
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
ไทย
ข้อผิดพลาดที่พบบ่อย
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE - The name of the file is a string, in double quotes.
English
Now you try
- Write a line, read it back, and
OUTPUTit. - Press Check answer to test it.
ไทย
ลองดูเลย
- Write a line, read it back, and
OUTPUTit. - กด Check answer เพื่อทดสอบ
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่