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
Open, then read or write
- AS topic 10.3 asks for pseudocode that handles a text file of one or more lines.
- Open the file in one mode:
READ,WRITEorAPPEND. WRITEstarts a new file and loses what was there.APPENDadds after what is there.CLOSEFILEwhen you have finished. A file is open in only one mode at a time.
עברית
פתח, ואז קרא או כתוב
- נושא AS 10.3 דורש קוד פסאודו שמטפל בקובץ טקסט בעל שורה אחת או יותר.
- פתח את הקובץ במצב אחד:
READ,WRITEאוAPPEND. WRITEמתחיל קובץ חדש ומאבד מה שהיה בו.APPENDמוסיף לאחר מה שהיה שם.CLOSEFILEכאשר סיימת. קובץ פתוח במצב אחד בלבד בכל עת.
OPENFILE "Note.txt" FOR WRITE
WRITEFILE "Note.txt", "Hello"
CLOSEFILE "Note.txt"
DECLARE LineOfText : STRING
OPENFILE "Note.txt" FOR READ
READFILE "Note.txt", LineOfText
CLOSEFILE "Note.txt"
OUTPUT LineOfText
English
EOF tells you when to stop
EOF("Note.txt")isTRUEwhen there is no further line to read.WHILE NOT EOF(...)reads every line.READFILEputs the next line into aSTRING.
עברית
EOF אומר לך מתי לעצור
EOF("Note.txt")הואTRUEכאשר אין עוד שורה לקריאה.WHILE NOT EOF(...)קורא כל שורה.READFILEמכניס את השורה הבאה לתוךSTRING.
OPENFILE "List.txt" FOR WRITE
WRITEFILE "List.txt", "a"
WRITEFILE "List.txt", "b"
CLOSEFILE "List.txt"
DECLARE LineOfText : STRING
OPENFILE "List.txt" FOR READ
WHILE NOT EOF("List.txt")
READFILE "List.txt", LineOfText
OUTPUT LineOfText
ENDWHILE
CLOSEFILE "List.txt"
English
Watch out
- Declare the string before
READFILEuses it. - Random files (
OPENFILE … FOR RANDOM) are A2. This lesson does not use them.
עברית
שימו לב
- הגדר את המחרוזת לפני ש-
READFILEמשתמש בה. - תיקיות אקראיות (
OPENFILE … FOR RANDOM) הן A2. ערך זה אינו משתמש בהן.
English
Common mistakes
- Read with
READFILEonly afterOPENFILE … FOR READ. WRITEreplaces the file. UseAPPENDto keep the old lines.
עברית
טעויות נפוצות
- קרא עם
READFILEרק לאחר ש-OPENFILE … FOR READפתח את הקובץ. WRITEמחליף את הקובץ. השתמש ב-APPENDכדי לשמור את השורות הישנות.
English
Now you try
- Write two lines, then read them back until
EOF.
עברית
כעת תנסו בעצמכם
- כתוב שתי שורות, ואז קרא אותן חזרה עד ש-
EOFמחזיר סוף.
Write Hello to Note.txt, read it back into LineOfText, and output it.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write a and b to List.txt, then output both lines using WHILE NOT EOF.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write one to Note.txt, close it, APPEND the line two, then read both lines with WHILE NOT EOF and output them.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.