File processing
แนะนำใหม่
13:52
แนวทางการเขียนโปรแกรม (Programming Paradigms)
นี่คืองานหนึ่ง: รายการนักเรียนทุกคนที่ได้เกรด 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 เมื่อถึงท้ายไฟล์แล้ว- เลือกโครงสร้างไฟล์และวิธีการเข้าถึงก่อน: ไฟล์แบบลำดับ, แบบเรียงลำดับ และแบบสุ่ม ทำหน้าที่ต่างกัน ตัวอย่างการใช้ copy-through ในที่นี้ใช้การเข้าถึงแบบเรียงลำดับสำหรับเรคอร์ดข้อความ
- รูปแบบสำหรับการอ่านไฟล์ทั้งฉบับมีรูปแบบเดียวกันเสมอ:
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 - The
foundflag matters. Without it the loop reads the whole file even after a match, which is wasteful, and there is nothing to test afterwards to report "not 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: เนื้อหาทั้งหมดที่มีอยู่จะถูกทำลายทันทีที่ไฟล์ถูกเปิด ก่อนที่จะมีการเขียนบรรทัดใดเลย
- Reading past 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, และมีสิ่งที่ทดสอบ afterwards - WRITE ทับ; APPEND เพิ่ม เลือกผิดจะทำลายไฟล์ทันทีที่เปิด
- เสมอ
CLOSEFILEบอกเหตุผล: ข้อมูลในบัฟเฟอร์อาจสูญเสียหากไม่ทำอย่างนั้น
คุณเข้าใจแล้ว
OPENFILE … FOR READ | WRITE | APPEND, จากนั้นREADFILEหรือWRITEFILE, แล้วCLOSEFILE;EOFถูกทดสอบ ก่อน การอ่านทุกครั้ง- WRITE สร้างหรือ ทับ, APPEND เก็บเนื้อหาและเพิ่มที่ท้าย, READ เริ่มจากต้น
- ค้นหาด้วย
WHILE NOT EOF AND NOT found, เพื่อให้ลูปหยุดเร็วขึ้นและรายงาน "ไม่พบ" ได้ - อัปเดตโดยการคัดลอกผ่าน ไฟล์ชั่วคราว, เขียนแต่ละบรรทัดที่แก้ไขหรือไม่แก้ไข, ข้ามบรรทัดเพื่อลบหรือเพิ่มหนึ่งบรรทัดเพื่อใส่, แล้ว แทนที่ไฟล์ต้นฉบับ
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้