File processing
הוצג
13:52
פרדיגמות תכנות
הנה משימה אחת: רשימת כל התלמידים עם ציון A. תוכנתן כותבת לולאה — מתחילה מהרקורד הראשון, בודוק את הציון, הוסף את השם לרשימה, עבור…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| text file/tekst faɪl/ | A file that stores data as a sequence of encoded characters. |
| stream of characters | A sequence of characters stored one after another in a file. |
| byte/baɪt/ | A group of eight bits, commonly used as a unit of data storage. |
| file operations | Actions a program performs on files, such as reading and writing. |
| APPEND/əˈpend/ | הוספה |
| file layout | The way data and records are organised within a file. |
| access method/ˈækses ˈmeθəd/ | A way of reaching and reading records stored in a file. |
| sequential access/siːˈkwenʃl ˈækses/ | Reading records in order from the start until the wanted record is reached. |
| text records | Units of information stored as text, often one per line in a file. |
| flag/flæɡ/ | A stored indicator of a condition or state, often represented by a Boolean value or a single bit. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| temporary file/ˈtemprəri faɪl/ | קובץ זמני |
| sequential update | Changing records by reading them in order and writing the updated results. |
| record/ˈrekɔːd/ | A stored collection of information about an item, person or event. |
| delete/dɪˈliːt/ | To remove a record from a table. |
| insert/ˈɪnsɜːt/ | To add a new item to a data structure, file or table. |
| Hard-coded paths | Put the filename in a constant so it appears once and the program can be moved. |
| filename | The name used to identify a file when accessing it. |
| constant/ˈkɒnstənt/ | A named or literal value that does not change during the relevant part of a program. |
| mode/məʊd/ | A setting that determines how a file is opened or used, such as reading, writing or appending. |
| log/lɒɡ/ | A record of events and actions on a computer system. |
| overwrite/ˌəʊvəˈraɪt/ | כתיבה חוזרת |
| configuration file | A file that stores settings a program uses when it runs. |
לא ניתן לשנות שורה אחת בתיקון טקסט
- תוכנית צריכה לתקן סילוף במרכז קובץ לקוחות. נראה הגיוני: למצוא את השורה, לשנות אותה, ולשמור.
- כמעט שום שפה לא תאפשר זאת. קובץ טקסט הוא זרם של תווים, ושם המתוקן הוא באורך שונה מהשם הישן, ולכן כל בייט שלאחריו צריך יהיה להזיז.
- לכן התשובה הסטנדרטית היא לא לערוך בכלל: העתק את הקובץ כולו, תוך שינוי שורה אחת בזמן מעבר, והנח את ההעתק במקום המקורי.
- שיעור זה עוסק בפעולות קובץ, בדוגמה של העתקה דרך מעבר כדי עדכון, ובמכשולים הגורמים לבגים אמיתיים.

הפעולות
OPENFILE name FOR READ | WRITE | APPENDפותח את הקובץ. READ פותח קובץ קיים בתחילה; WRITE יוצר אותו, או מוחק קובץ קיים; APPEND פותח אותו ומוסיף בסוף.READFILE name, variableקורא את השורה הבאה.WRITEFILE name, valueכותב שורה אחת.CLOSEFILE nameסוגר אותה.EOF(name)הוא TRUE כאשר הגיעו לסוף.- בחרו תחילה את עיצוב הקובץ ואת שיטת הגישה: קבצים סריים, רצופים ואקראיים משמשים הפעלות שונות. דוגמאות העתקה-דרכן כאן משתמשות בגישה רציפה לרשומות טקסט.
- הדפוס לקריאת קובץ שלם הוא תמיד אותו דבר:
OPENFILE "names.txt" FOR READ
WHILE NOT EOF("names.txt") DO
READFILE "names.txt", thisName
OUTPUT thisName
ENDWHILE
CLOSEFILE "names.txt"

פתח, השתמש, סגור, בכל פעם


File access route
Follow a file from storage to program and back safely.
What does opening a file FOR WRITE do to existing contents?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
WRITE creates or overwrites the file. To keep existing contents and add more, use APPEND.
Match each file operation to what it does.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
WRITE overwrites, APPEND adds; EOF marks the end; the search loop stops early once the value is found.
חיפוש בקובץ
- כדי לחפש, לקרוא שורה אחר שורה ולעצור כאשר נמצאת המטרה. נדרשים שני תנאים:
WHILE NOT EOF(…) AND NOT found. - הדגל
foundחשוב. ללאו, הלופ יקרא את כל הקובץ גם לאחר התאמה, מה שהוא חסכוני, ואין מה לבדוק לאחר מכן לדיווח "לא נמצא".
found ← FALSE
OPENFILE "names.txt" FOR READ
WHILE NOT EOF("names.txt") AND NOT found DO
READFILE "names.txt", thisName
IF thisName = target THEN
found ← TRUE
ENDIF
ENDWHILE
CLOSEFILE "names.txt"

When searching a file for a value, the loop condition usually includes:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Stopping when found avoids reading the rest of the file unnecessarily.
Why does a file search loop use WHILE NOT EOF AND NOT found? Select כולם that apply.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Every file must still be closed. The two conditions handle early exit and the end of the file respectively.
עדכון: דפוס הקובץ הזמני
- מכיוון שקובץ טקסט אינו ניתן לעריכה במוקום, עדכון משתמש בקובץ זמני.
- פתח את המקורי FOR READ וקובץ זמני FOR WRITE. קרא כל שורה בתור; כתוב אותה לקובץ הזמני או שונה או לא שונה.
- סגור את שני הקבצים, ולאחר מכן החלף את המקורי עם הקובץ הזמני.
- אותו דפוס עושה הכל: ל-מחוק רשומה, דלג על כתיבת שורה זו; ל-הכנס אחת, כתוב את השורה החדשה בנקודה הנכונה בזמן העתקה.

To update a text file when in-place editing is not possible, you:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Copy each line (changed or not) to a temp file, then swap it in for the original.
Put the steps of deleting one record from a text file in order.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
The last step is the one candidates leave out, and without it nothing has changed. The same pattern inserts or edits a record.
דוגמה פותרת: מחיקת רשומה
- תאר כיצד תוכנה מחקה רשומה אחת מקובץ טקסט. [4]
- פתח את הקובץ המקורי לקריאה וקובץ זמני לכתיבה.
- קרא כל שורה בתור. אם השורה אינה זו שמחקים, כתוב אותה לקובץ הזמני; אם כן, אל תכתוב אותה.
- כאשר מגיעים לסוף המקורי, סגור את שני הקבצים.
- החלף את הקובץ המקורי עם הקובץ הזמני. ארבעת השלבים זוכים נקודות, והשלב האחרון הוא זה שהמועמדים שוכחים.

A program writes 500 records then ends without closing the file, and the last few are missing. Why?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Writes are buffered for speed. Closing the file flushes the buffer, and it may also release a lock other programs are waiting on.
הפחזות
- שכחת סגירה של קובץ: נתונים במבפר עשויים לעולם לא להיכתב, כך שהרשומות האחרונות נעלמות, והקובץ עשוי להישאר נעול מול תוכניות אחרות.
- פתיחה בFOR WRITE כאשר התכוונו ל-APPEND: כל תוכן הקובץ הקיים נמחק מיד ברגע הפתיחה, לפני כתיבת קו אחד.
- קריאה מעבר לסוף הקובץ (EOF): בדוק
EOFלפני כל פעולת קריאה, ולא אחריה. - נתיבים קבועים בתוכנה: השתמש בקבוע שם קובץ כך שיתופיע במקום אחד והתוכנית תוכל להעברה למחשב אחר.
Forgetting to close a file can lose buffered writes and lock other programs out — so you should always close a file when finished.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Closing flushes buffered data to disk and releases the lock — a FINALLY block is a good place to guarantee it.
To add today's entries to a log that must keep its history, open the file FOR ____.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
WRITE would destroy every previous entry the instant the file was opened, before a single line was written.
דוגמה מפורטת: איזה מצב
- תוכנית מוסיפה עסקאות היום ליומן רציף שחייב לשמור את ההיסטוריה. APPEND: שימוש ב-WRITE ישמיד כל הקלטות הקודמות.
- תוכנית מייצרת דוח חדש כל לילה, תוך החלפת הדוח של אתמול. WRITE: יצירה או חילוף הם בדיוק מה שנדרש.
- תוכנית קוראת קובץ הגדרות בהפעלה. READ.
- המצב נבחר לפי מה שעובר על התוכן הקיים: שמירה והוספה, מחיקה, או השארה ללא שינוי.
סימונים נופלים מהיד
- דפוס העדכון מסתיים ב-החלפת המקורי בקובץ הזמני. העתקה ללא השלב הסופי הזה לא משנה דבר.
- לולאת חיפוש זקוקה לשתי התנאים,
NOT EOFוNOT found, ולמשהו לבדיקה לאחר מכן. - WRITE מחליף; APPEND מוסיף. בחירה שגויה תשמיד את הקובץ ברגע הפתיחה.
- תמיד
CLOSEFILE. הסבר: נתוני הBuffer עלולים להישכח אחרת.
הבנתך מושלמת
OPENFILE … FOR READ | WRITE | APPEND, ואזREADFILEאוWRITEFILE, ואזCLOSEFILE;EOFנבדק לפני כל קריאה- WRITE יוצר או מחליף, APPEND שומר על התוכן ומוסיף בסוף, READ מתחיל מהתחלה
- חפש עם
WHILE NOT EOF AND NOT found, כדי שהלולאה תיעצר מוקדם ו"לא נמצא" ידווח - עדכן על ידי העתקה דרך קובץ זמני, כתיבה בכל שורה ששונתה או נותרה ללא שינוי, דילוג על שורה למחיקה או הוספת שורה להכנסה, ואז החלפת המקורי
מה השתבש? רק צוות האתר רואה זאת.