ไฟล์
แนะนำใหม่
17:29
ประเภทข้อมูล & โครงสร้างข้อมูล
ทุกค่าที่คุณโปรแกรมจัดเก็บต้องมีประเภทข้อมูล — และการเลือกให้ถูกต้องนั้นสำคัญ Suppose คุณต้องการจัดเก็บว่าสินค้ามีในสต็อกหรือไม่ คุณอาจพิมพ์คำว่า yes…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| games console | A device that runs video games and connects to a screen. |
| cartridge | A removable device that stores a game for a games console. |
| bytes | Units of eight bits used to store data in a computer. |
| save/seɪv/ | To store data so that it can be used later. |
| document/ˈdɒkjuːmənt/ | A file containing information created or edited by a user. |
| RAM/ræm/ | Random-access memory used for working data and instructions, usually losing its contents when power is removed. |
| file/faɪl/ | ไฟล์ |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| text file/tekst faɪl/ | ไฟล์ข้อความ |
| Variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| secondary storage/ˈsekəndəri ˈstɔːrɪdʒ/ | หน่วยความจำสำรอง |
| persistence/pəˈsɪstəns/ | ความถาวร |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| mode/məʊd/ | A setting that determines how a file is opened or used, such as reading, writing or appending. |
| characters/ˈkærɪktəz/ | A text unit such as a letter, digit, punctuation mark or other symbol. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| overwrites | Replaces the existing contents of a file with new data. |
| end of file/end ɒv faɪl/ | สิ้นสุดไฟล์ |
| buffered data | Data held temporarily in memory before it is written to a file. |
| strings/strɪŋz/ | Sequences of characters used as text in a program. |
| procedure/prəˈsiːdʒə/ | A named sequence of instructions that performs a task when called. |
| empty strings | Text values that contain no characters at all. |
| fields/fiːldz/ | Parts of a database record that each hold one item of data. |
| record/ˈrekɔːd/ | A stored collection of information about an item, person or event. |
| separator/ˈsepəreɪtə/ | ตัวคั่น |
| Boolean/ˈbuːlɪən/ | A logical value that is either true or false. |
| escaping scheme | A set of rules for writing special characters so they are read correctly. |
| type/taɪp/ | A category of data that determines what values it can hold. |
| serial/ˈsɪərɪəl/ | Describes access to data in order, starting at the beginning of a file. |
| sequential/siːˈkwenʃl/ | ตามลำดับ |
| Direct (random) access | A way to reach a record at a calculated position without reading earlier records. |
| fixed-length records | Records that all take up the same amount of storage space. |
เกมที่จดจำได้
- จนถึงปี ค.ศ. 1987 การปิดเครื่องเกมจะลบทุกอย่างทิ้ง ทุกเซสชันจะเริ่มจากหน้าจอแรก เกมที่ยาวนานคือบ่ายที่ยาวนานหรือไม่เลยก็ได้
- The Legend of Zelda มาพร้อมกับแบตเตอรี่ขนาดเล็กภายใน cartridge ที่รักษาข้อมูลหลายร้อยไบต์ไว้ให้ทำงานต่อ ครั้งแรกเกมสำหรับบ้านสามารถ บันทึก และกลับมาเล่นต่อจากจุดที่เคยวางไว้ได้
- คะแนนสูงสุดทุกคะแนน เอกสารทุกฉบับ และการตั้งค่าที่คุณเคยเก็บไว้ทั้งหมดมีอยู่เพราะเหตุผลเดียวกัน: RAM ลืมเมื่อไฟดับ และ ไฟล์ ไม่ลืม
- This lesson is why files exist and how pseudocode reads and writes a text file, line by line.
ทำไมต้องใช้ไฟล์
- ตัวแปรอยู่ใน RAM และหายไปเมื่อโปรแกรมสิ้นสุด ไฟล์ บน พื้นที่จัดเก็บรอง รักษาข้อมูลไว้ระหว่างรอบการทำงานของโปรแกรม: ความถาวร (persistence)
- ไฟล์ทำให้ผลลัพธ์ของโปรแกรมหนึ่งกลายเป็นอินพุตของอีกโปรแกรมหนึ่ง ทำให้ข้อมูลสามารถ แชร์ ได้
- ไฟล์สามารถเก็บข้อมูลได้ มากกว่าที่หน่วยความจำรองรับ และโปรแกรมสามารถ เริ่มใหม่ จากสถานะที่บันทึกไว้

ตัวแปรหายไปเมื่อจบรอบการทำงาน; ไฟล์ยังอยู่พรุ่งนี้

Why does a program write data to a file rather than keeping it in a variable?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
RAM is volatile, so data is lost on exit. A file on secondary storage persists between runs.
ไฟล์ข้อความและโหมดทั้งสาม
- ไฟล์ข้อความ เก็บข้อมูลเป็นบรรทัดของตัวอักษร ซึ่งโปรแกรมอ่านและเขียนทีละบรรทัด เริ่มจากต้น ลำดับของบรรทัดเหล่านี้คือสิ่งที่ทำให้ลูปสามารถวนผ่านได้
OPENFILE … FOR READอ่านจากบรรทัดแรก.FOR WRITEสร้างไฟล์ใหม่หรือ เขียนทับ ไฟล์ที่มีอยู่.FOR APPENDเพิ่มบรรทัดใหม่ที่ ปลาย โดยรักษาสิ่งที่มีอยู่แล้ว- เปิดก่อนใช้ ปิดหลังใช้ ไฟล์จะมีเพียงโหมดเดียวในเวลาใดเวลาหนึ่ง

Match each file-open mode to what it does.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
READ reads; WRITE replaces from scratch; APPEND adds to the end without losing what is there.
To add lines to the end of an existing file without losing its contents, open it FOR ____.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
WRITE would start the file empty. APPEND keeps what is there and writes after it.
การอ่านทุกบรรทัด
OPENFILE "data.txt" FOR READ
WHILE NOT EOF("data.txt") DO
READFILE "data.txt", LineString
OUTPUT LineString
ENDWHILE
CLOSEFILE "data.txt"
EOFทดสอบ ปลายไฟล์ ทดสอบมัน ก่อน การอ่านทุกครั้ง: การอ่านเกินปลายไฟล์ถือเป็นข้อผิดพลาดCLOSEFILEสำคัญ: ข้อมูลที่เก็บชั่วคราวอาจสูญหายและโปรแกรมอื่นไม่สามารถเข้าถึงไฟล์ที่ยังเปิดอยู่ได้
Handling a file: open → use → close
Step through the lifecycle every file follows. The two easy-to-forget parts are testing EOF while reading in a loop, and always closing at the end.
What does EOF test for?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
EOF (end of file) is checked before each read so the loop stops when there is no more data.
Why must you always close a file after using it?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Closing flushes buffered writes to disk and releases the lock so other programs can use the file.
การเขียนบรรทัด
OPENFILE "log.txt" FOR WRITE
FOR i ← 1 TO 100
WRITEFILE "log.txt", "Event " & NUM_TO_STR(i)
NEXT i
CLOSEFILE "log.txt"
WRITEเริ่มด้วยไฟล์ว่าง. เพื่อเพิ่มลงในบันทึกที่ต้องรักษาประวัติให้เปิดFOR APPENDแทน- ไฟล์ข้อความเก็บ สตริง: ตัวเลขจะถูกแปลงด้วย
NUM_TO_STRในการส่งออก
ตัวอย่างวิธีทำ: สามบรรทัดสุดท้ายของไฟล์
- เขียน procedure
LastLines(FileName)เพื่อแสดงผลสามบรรทัดสุดท้ายของไฟล์ข้อความ ตามลำดับ
PROCEDURE LastLines(BYVAL FileName : STRING)
DECLARE LineX, LineY, LineZ : STRING
LineX ← ""
LineY ← ""
LineZ ← ""
OPENFILE FileName FOR READ
WHILE NOT EOF(FileName) DO
LineX ← LineY
LineY ← LineZ
READFILE FileName, LineZ
ENDWHILE
CLOSEFILE FileName
OUTPUT LineX
OUTPUT LineY
OUTPUT LineZ
ENDPROCEDURE
- แต่ละบรรทัดใหม่จะดันบรรทัดเก่าออกไป ทำให้ที่
EOFตัวแปรทั้งสามจะเก็บสามบรรทัดสุดท้าย; ไฟล์สั้นกว่าจะแสดงผลเป็นสตริงว่าง สำหรับ ห้า บรรทัดแรก นับจำนวนการอ่านและหยุดที่ห้า หรือ ที่EOFอย่างใดอย่างหนึ่งที่เกิดขึ้นก่อน ไฟล์ว่างจะทำให้EOFเป็นจริงทันทีหลังการเปิด

Put the steps of the LastLines procedure in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Initialise, open, loop with the shift before the read, close, output. Shifting after the read would lose the newest line.
ช่องในบรรทัด
- A record is written as one line, its fields joined by a separator character, and each number or Boolean converted with
NUM_TO_STR(and read back withSTR_TO_NUM, or by comparing with"TRUE"). - เลือกตัวคั่นที่จะ ไม่มีวันปรากฏในข้อมูล: เติมหรือ
|สำหรับชื่อและตัวเลข; อย่าใช้เว้นวรรคเมื่อชื่ออาจมีช่องว่าง - การอ่านกลับ: อ่านบรรทัดนั้น, แยกด้วยตัวคั่น, แปลงแต่ละฟิลด์ให้เป็นประเภทของข้อมูล, แล้วจัดเก็บลงในเรคคอร์ด

เชื่อมต่อกันด้วยตัวคั่นเพื่อเขียน; แยกและแปลงประเภทเพื่ออ่าน

A line stores a player's full name and score. Which separator is the safest choice?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The separator must never occur inside a field. A full name contains a space, and a score contains digits.
An INTEGER can be written to a text file directly, without converting it to a string first.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A text file holds lines of characters. Convert with NUM_TO_STR to write and STR_TO_NUM to read back.
ตัวอย่างวิธีทำ: บันทึกเรคคอร์ดแล้วโหลดกลับมา
- A record
Playerhas fieldsName : STRINGandScore : INTEGER. Write pseudocode to append it toscores.txtas one line, and to read one line back into the record.
OPENFILE "scores.txt" FOR APPEND
WRITEFILE "scores.txt", Player.Name & "," & NUM_TO_STR(Player.Score)
CLOSEFILE "scores.txt"
OPENFILE "scores.txt" FOR READ
READFILE "scores.txt", Line
Pos ← POSITION(Line, ",")
Player.Name ← LEFT(Line, Pos - 1)
Player.Score ← STR_TO_NUM(MID(Line, Pos + 1, LENGTH(Line) - Pos))
CLOSEFILE "scores.txt"
- คะแนน: โหมดที่ถูกต้อง, การแปลงตัวเลขในทั้งสองทิศทาง, ตัวคั่นที่ไม่สามารถปรากฏในชื่อ, และการปิดไฟล์ทั้งสอง

To store a record as one line of a text file, a program must: select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Convert, join, write, close. A text file has no binary layout; that is what a binary file would do.
การเข้าถึงแบบลำดับและการเข้าถึงโดยตรง
- ไฟล์ข้อความเป็นแบบ ลำดับ (เรียงลำดับ): จะอ่านตามลำดับตั้งแต่ต้น ดังนั้นการหาบรรทัดหนึ่งจึงต้องอ่านทุกบรรทัดก่อนหน้า
- การเข้าถึง โดยตรง (สุ่ม) จะกระโดดไปยังเรคคอร์ดที่ตำแหน่งที่คำนวณไว้โดยไม่ต้องอ่านอื่นๆ; จำเป็นต้องใช้เรคคอร์ดความยาวคงที่ ซึ่งไฟล์ข้อความไม่มี
- สำหรับหลักสูตร การวนลูปไฟล์ข้อความข้างต้นคือโมเดล: เปิด, ทดสอบ
EOF, อ่าน, ปิด

Direct (random) access lets a program jump straight to a record without reading the others first.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Serial access must read from the start.
คะแนนที่หลุดหายไป
- การอ่านเกินปลายไฟล์ ทดสอบ
EOFก่อน การกระทำREADFILEทุกครั้ง - เปิด
FOR WRITEเมื่อไฟล์ต้องรักษาเนื้อหาไว้ นั่นคือAPPEND. - การเขียนตัวเลขโดยไม่ใช้
NUM_TO_STR, หรืออ่านกลับโดยไม่ใช้STR_TO_NUMไฟล์ข้อความจัดเก็บเป็นสตริงเท่านั้น - ลืม
CLOSEFILE, หรือใช้ไฟล์ก่อนOPENFILEทั้งสองกรณีจะเสียคะแนนเรื่องการจัดการไฟล์
คุณเข้าใจแล้ว
- ไฟล์ให้ความ ถาวร: ข้อมูลยังคงอยู่หลังจบโปรแกรม สามารถแชร์ได้ และสามารถมีปริมาณมากกว่าหน่วยความจำ
OPENFILE … FOR READ | WRITE | APPEND→READFILE/WRITEFILE→CLOSEFILE;WRITEทับ,APPENDเพิ่มที่ท้าย- ทดสอบ
EOFก่อนการอ่านทุกครั้ง; แปลงตัวเลขโดยใช้NUM_TO_STRออกและSTR_TO_NUMกลับ - หนึ่งเรคคอร์ด = หนึ่งบรรทัด, ฟิลด์เชื่อมต่อกันด้วย ตัวคั่น ที่ไม่ปรากฏในข้อมูล
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้