Text files · Tệp văn bản
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
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.
Tiếng Việt
A file giữ dữ liệu sau khi chương trình dừng
- Chương trình 2026–2028 (mục 8.3) lưu dữ liệu trong một tệp văn bản.
- Bạn mở tệp, đọc hoặc ghi một mục, sau đó đóng nó.
- Khóa học này giữ tệp trong bộ nhớ cho lần chạy duy nhất, để bạn có thể xem kết quả.
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.
Tiếng Việt
READ và WRITE
OPENFILE "name.txt" FOR READđọc những gì đã tồn tại.OPENFILE "name.txt" FOR WRITEbắt đầu một tệp mới. Everything có sẵn bên trong sẽ bị mất.READFILEđưa mục tiếp theo vào một biến.WRITEFILElưu một giá trị.CLOSEFILEkhi bạn hoàn thành. Một tệp chỉ được mở ở một chế độ duy nhất tại một thời điểm.
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.
Tiếng Việt
Đọc từng dòng
EOF("List.txt")làTRUEkhi không còn dòng nào để đọc nữa.WHILE NOT EOF(...)đọc toàn bộ tệp. MỗiREADFILEđưa dòng tiếp theo vào mộtSTRING.- Đóng tệp khi bạn hoàn tất. Một lần mở sau đó có thể sử dụng nóared.
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.
Tiếng Việt
Lưu ý
- Khai báo biến trước khi
READFILEvào nó. WRITEghi đè lên tệp. Đọc nó trước nếu bạn vẫn cần văn bản cũ.
English
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
Tiếng Việt
Lỗi thường gặp
- Mở cho
READhoặcWRITE, sau đóREADFILEhoặcWRITEFILE, sau đóCLOSEFILE. - Tên của tệp là một chuỗi, trong dấu ngoặc kép đôi.
English
Now you try
- Write a line, read it back, and
OUTPUTit. - Press Check answer to test it.
Tiếng Việt
Bây giờ bạn thử
- Ghi một dòng, đọc nó trở lại, và
OUTPUTnó. - Nhấn Kiểm tra đáp án để thử nghiệm.
Write the line Hello to Note.txt, read it back into LineOfText, and output it. · Viết dòng Hello vào Note.txt, đọc nó trở lại vào LineOfText, và xuất nó.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Store Saved in FileA.txt, read it into LineOfText, write that same text to FileB.txt, and output it. · Lưu Saved vào FileA.txt, đọc nó vào LineOfText, ghi cùng đoạn văn bản đó vào FileB.txt, và xuất ra màn hình.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write Cat and Dog to List.txt, then output both lines with WHILE NOT EOF. · Viết Cat và Dog vào List.txt, sau đó xuất cả hai dòng bằng WHILE NOT EOF.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.