title: Why is my code causing a I/O 104 error?
body: <p>This program raises an I/O 104 error on <code>EoF</code> when first entering the while loop.</p>
  
  <p>The purpose of the program is to look up if a username is already taken. The existing usernames are stored in a text file.</p>
  
  <pre><code>procedure TForm1.btnRegisterClick(Sender: TObject);
  begin
    sCUser := edtUserName.Text;
    AssignFile(tNames, 'Names.txt');
    begin
      try
        Reset(tNames);
      except
        ShowMessage('File not found');
        Exit;
      end;
    end;
    rewrite(tNames);
    while not EoF(tNames) do // I get a I/O 104 Error here `
    begin
      Readln(tNames, sLine);
      iPosComme := Pos(',', sLine);
      sUser     := Copy(sLine, 1, iPosComme - 1);
      Delete(sLine, 1, iPosComme - 1);
      if sCUser = sUser then begin
        ShowMessage('Username taken');
      end
      else
      begin
        rewrite(tNames);
        Writeln(tNames, sCUser + ',' + '0');
        CloseFile(tNames);
      end;
    end;
  end;
  </code></pre>