Text Files Grade 11
Text files allow your Delphi program to save and load data that persists between runs. Learn the four operations: reading, writing, appending, and checking existence.
What Is a Text File?
A text file is a file that stores plain, readable characters with no special formatting — just letters, numbers and line breaks, exactly as you would type them into Notepad. There are no fonts, colours, or hidden layout codes like you would find in a Word document; it is simply text, one line after another.
Think of a text file as a plain notepad. You can open it, read what is written, add a new line at the bottom, or tear out the page and start fresh. There is nothing fancy inside — just the words you wrote.
Why Do We Use Files?
The variables in your program only live in memory (RAM) while the program is running. The moment you close the program, everything in those variables is gone. A file gives your data persistence — it is saved onto the disk so that it survives after the program closes and is still there the next time you run it.
Example: a marks program that lets a teacher capture results is useless if every mark vanishes when she closes it. By writing the marks to a text file, the data is safely stored and can be read back tomorrow.
Physical vs Logical Filename
When working with files in Delphi there are two "names" to keep straight:
- Physical filename – the real name of the file as it exists on the disk, for example
'Data.txt'. This is what you would see in File Explorer. - Logical filename – the file variable inside your program (of type
TextFile), for exampleF. Your code talks to this variable, not directly to the disk.
The AssignFile command is what links the two together — it tells Delphi "whenever I use the logical variable F, I really mean the physical file Data.txt on the disk".
Think of a postbox. The physical filename is the actual postbox standing in the street with a real street address. The logical filename is the label F you stick on your desk that says "this drawer represents that postbox". AssignFile is the act of writing the street address onto your label so the two are connected.
What Each File Command Does
Before looking at the table, here is each command explained in plain words:
- AssignFile(F, 'file.txt') – links your file variable to the real file on disk. Always the very first step.
- Reset(F) – opens an existing file so you can read from it, starting at the very beginning.
- Rewrite(F) – creates a brand-new empty file for writing. If the file already exists, its old contents are wiped out, so use it with care.
- Append(F) – opens an existing file and moves to the very end, so new lines are added without destroying what is already there.
- ReadLn(F, S) – reads one whole line from the file into the variable
S, then moves on to the next line. - WriteLn(F, S) – writes the contents of
Sinto the file as one line. - CloseFile(F) – saves and properly closes the file. Like shutting and locking the postbox so nothing is lost.
- Eof(F) – short for "End Of File". Returns
Trueonce you have read the last line, so your loop knows when to stop.
Key Procedures and Functions
| Command | Purpose |
|---|---|
AssignFile(F, 'file.txt') | Links the file variable F to a file on disk |
Reset(F) | Opens existing file for reading (from beginning) |
Rewrite(F) | Creates/overwrites a file for writing |
Append(F) | Opens existing file for writing — adds to end |
CloseFile(F) | Saves and closes the file |
ReadLn(F, S) | Reads one line from file into variable S |
WriteLn(F, S) | Writes variable S as one line to the file |
EOF(F) | Returns True when the end of the file is reached |
FileExists('file.txt') | Returns True if the file exists on disk |
Reading a Text File
var
F: TextFile;
S: string;
begin
if FileExists('Data.txt') then
begin
AssignFile(F, 'Data.txt');
Reset(F); // open for reading
while not EOF(F) do
begin
ReadLn(F, S);
memOut.Lines.Add(S); // process each line
end;
CloseFile(F);
end
else
ShowMessage('File not found!');
end;Writing to a Text File (Overwrite)
var
F: TextFile;
begin
AssignFile(F, 'Output.txt');
Rewrite(F); // creates or overwrites
WriteLn(F, sName + ',' + sSurname + ',' + IntToStr(iAge));
CloseFile(F);
end;Appending to a Text File
var
F: TextFile;
begin
AssignFile(F, 'Data.txt');
Append(F); // adds to existing file
WriteLn(F, 'New data line');
CloseFile(F);
end;Reading CSV Data into Arrays
var
F: TextFile;
sLine, sName: string;
iMark, iCount, iPos: Integer;
begin
iCount := 0;
AssignFile(F, 'marks.txt');
Reset(F);
while not EOF(F) do
begin
ReadLn(F, sLine);
iPos := Pos(',', sLine);
Inc(iCount);
arrNames[iCount] := Copy(sLine, 1, iPos - 1);
arrMarks[iCount] := StrToInt(Copy(sLine, iPos + 1, Length(sLine)));
end;
CloseFile(F);
end;Forgetting CloseFile(F) means the file remains locked and data may not be saved properly.