File processing
導入済み
13:52
プログラミングのパラダイム
ここで一つのタスクがあります:A gradeを取得した生徒をすべてリストアップすることです。あるプログラマーはループを書きます——最初のレコードから始め、gradeを確認し、名前をリストに加え、移動します…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| 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. |
テキストファイルの一行のみを変更することはできない
- プログラムは顧客ファイルの真ん中のスペルミスを修正する必要があります。一見すると明白です:行を見つけ、変更し、保存します。
- ほぼどの言語でも、ファイルの特定の位置を直接編集することはできません。テキストファイルは文字の列であり、修正後の名前は旧名の長さとは異なるため、その後のすべてのバイトが移動しなければなりません。
- そこで標準的な解答は、編集しないことです:ファイル全体をコピーし、通過する際にその1行だけを変更して、それを元のファイルに置き換えます。
- このレッスンは、ファイル操作、更新のためのコピー・スルー・パターン、そして実際のバグを引き起こす罠についてです。

操作
OPENFILE name FOR READ | WRITE | APPENDはファイルを開きます。READ は既存のファイルを先頭から開き、WRITE は作成するか、既存のファイルを上書きします、APPEND は開いて末尾に追加します。READFILE name, variableは次の行を読み取ります。WRITEFILE name, valueは1行を書きます。CLOSEFILE nameは閉じます。EOF(name)は到達した時に TRUE です。- ファイルレイアウトとアクセス方法を最初に選択してください:serial, sequential and random files は異なる操作に適しています。ここでは text records に対する sequential access を用いた 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.
ファイルの検索
- 検索するには、ラインバイラインで読み進め、ターゲットが見つかったら停止します。2つの条件が必要です:
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.
例題:レコードを削除する
- プログラムがテキストファイルから1つのレコードを削除する方法を説明しなさい。 [4]
- 元のファイルを読み取り専用で開き、一時ファイルを書込み専用で開きます。
- 各行を順に読みます。その行が削除対象でない場合、一時ファイルに書きます;もし削除対象である場合、書かないでください。
- 元のファイルの終端に到達したら、両方のファイルを閉じます。
- 元のファイルを一時ファイルで置き換えます。4つのステップすべてが採点対象であり、最後の1つは受験者がよく見落としているものです。

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.
罠
- ファイルを閉じない: バッファされたデータが書き込まれない場合があり、最終レコードが消え、他のプログラムによるアクセスがロックされた状態が続くことがあります。
- APPENDの意図に対しFOR WRITEを開く場合、ファイルが開かれた瞬間に既存の全内容が破棄され、最初の行が書かれる前である。
- 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を行ってください。理由を述べよ:否则バッファされたデータが失われる可能性があるため。
理解できましたか
OPENFILE … FOR READ | WRITE | APPEND、次にREADFILEまたはWRITEFILE、次にCLOSEFILE;EOFは各読み取りの前にテストされる- WRITE は作成または上書きし、APPEND は内容を保って末尾に追加し、READ は先頭から始める
WHILE NOT EOF AND NOT foundで検索し、ループが早期に停止して「見つからない」を報告できるようにする- 一時ファイルを介してコピーして更新し、各行を変化させて書込むか変化せず書込むか、削除するには行をスキップし、挿入するには追加し、最後に元のファイルを置き換える
何が問題でしたか?この内容はサイトの運営チームだけが確認します。