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.

ANALOGY

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:

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".

ANALOGY

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:

Key Procedures and Functions

CommandPurpose
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

Delphi — read all lines
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)

Delphi — create / 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

Delphi — add to end
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

Delphi — read Name,Mark pairs
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;
Always CloseFile!

Forgetting CloseFile(F) means the file remains locked and data may not be saved properly.