Reading from file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Reading from file

Hi!
I came across a stupid problem and I really have no idea why it arises. There is the following type declaration in my code:

db_settings=record
default_db:boolean;
filename:string[100];
server:string[100];
db:string[100];
user:string[100];
password:string[100];
end;

Then I write these things to a file:

procedure TForm2.Button10Click(Sender: TObject);
var
db_sett:db_settings;
dbsfile:file of db_settings;
begin
assignfile(dbsfile,'db_settings.dat');
with db_sett do
begin
filename:=edit1.Text;
server:=edit2.Text;
db:=edit3.Text;
user:=Edit4.Text;
password:=Edit5.Text;
if radiobutton1.Checked then default_db:=true else default_db:=false;
rewrite(dbsfile);
end;
end;

The next code reads from that file:

procedure TForm2.FormCreate(Sender: TObject);
var
db_sett:db_settings;
dbsfile:file of db_settings;
begin
if fileexists('db_settings.dat') then
begin
assignfile(dbsfile,'db_settings.dat');
reset(dbsfile);
read(dbsfile,db_sett);
with db_sett do
begin
Edit1.Text:=filename;
Edit2.Text:=server;
Edit3.Text:=db;
Edit4.Text:=user;
Edit5.Text:=password;
if default_db then radiobutton1.Checked:=true else radiobutton2.Checked:=true;
end;
end;
end;

And here is the problem - I get 'Read beyon end of file.' each time the procedure tries to read from the file.

Comments

  • jamesb800jamesb800 Posts: 105Member
    : Hi!
    : I came across a stupid problem and I really have no idea why it arises. There is the following type declaration in my code:
    :
    : db_settings=record
    : default_db:boolean;
    : filename:string[100];
    : server:string[100];
    : db:string[100];
    : user:string[100];
    : password:string[100];
    : end;
    :
    : Then I write these things to a file:
    :
    : procedure TForm2.Button10Click(Sender: TObject);
    : var
    : db_sett:db_settings;
    : dbsfile:file of db_settings;
    : begin
    : assignfile(dbsfile,'db_settings.dat');
    : with db_sett do
    : begin
    : filename:=edit1.Text;
    : server:=edit2.Text;
    : db:=edit3.Text;
    : user:=Edit4.Text;
    : password:=Edit5.Text;
    : if radiobutton1.Checked then default_db:=true else default_db:=false;
    : rewrite(dbsfile);
    [red]closefile(dsbfile)[/red]
    : end;
    : end;
    :
    : The next code reads from that file:
    :
    : procedure TForm2.FormCreate(Sender: TObject);
    : var
    : db_sett:db_settings;
    : dbsfile:file of db_settings;
    : begin
    : if fileexists('db_settings.dat') then
    : begin
    : assignfile(dbsfile,'db_settings.dat');
    : reset(dbsfile);
    [red]While not EOF(dbsfile) do[/red]
    : read(dbsfile,db_sett);
    : with db_sett do
    : begin
    : Edit1.Text:=filename;
    : Edit2.Text:=server;
    : Edit3.Text:=db;
    : Edit4.Text:=user;
    : Edit5.Text:=password;
    : if default_db then radiobutton1.Checked:=true else radiobutton2.Checked:=true;
    : end;
    [red]closefile(dbsfile);[/red]
    : end;
    : end;
    :
    : And here is the problem - I get 'Read beyon end of file.' each time the procedure tries to read from the file.
    :

    I think you need to close the file and maybe use the EOF function to make sure that you are not trying to read beyond the end of the file.
    See your code in red. Zibadian might have a better answer for you.

    James

  • zibadianzibadian Posts: 6,349Member
    : : Hi!
    : : I came across a stupid problem and I really have no idea why it arises. There is the following type declaration in my code:
    : :
    : : db_settings=record
    : : default_db:boolean;
    : : filename:string[100];
    : : server:string[100];
    : : db:string[100];
    : : user:string[100];
    : : password:string[100];
    : : end;
    : :
    : : Then I write these things to a file:
    : :
    : : procedure TForm2.Button10Click(Sender: TObject);
    : : var
    : : db_sett:db_settings;
    : : dbsfile:file of db_settings;
    : : begin
    : : assignfile(dbsfile,'db_settings.dat');
    : : with db_sett do
    : : begin
    : : filename:=edit1.Text;
    : : server:=edit2.Text;
    : : db:=edit3.Text;
    : : user:=Edit4.Text;
    : : password:=Edit5.Text;
    : : if radiobutton1.Checked then default_db:=true else default_db:=false;
    : : rewrite(dbsfile);
    : [red]closefile(dsbfile)[/red]
    : : end;
    : : end;
    : :
    : : The next code reads from that file:
    : :
    : : procedure TForm2.FormCreate(Sender: TObject);
    : : var
    : : db_sett:db_settings;
    : : dbsfile:file of db_settings;
    : : begin
    : : if fileexists('db_settings.dat') then
    : : begin
    : : assignfile(dbsfile,'db_settings.dat');
    : : reset(dbsfile);
    : [red]While not EOF(dbsfile) do[/red]
    : : read(dbsfile,db_sett);
    : : with db_sett do
    : : begin
    : : Edit1.Text:=filename;
    : : Edit2.Text:=server;
    : : Edit3.Text:=db;
    : : Edit4.Text:=user;
    : : Edit5.Text:=password;
    : : if default_db then radiobutton1.Checked:=true else radiobutton2.Checked:=true;
    : : end;
    : [red]closefile(dbsfile);[/red]
    : : end;
    : : end;
    : :
    : : And here is the problem - I get 'Read beyon end of file.' each time the procedure tries to read from the file.
    : :
    :
    : I think you need to close the file and maybe use the EOF function to make sure that you are not trying to read beyond the end of the file.
    : See your code in red. Zibadian might have a better answer for you.
    :
    : James
    :
    :
    Nope. I don't have a better answer. This (including James's improvements) should work fine. I just found one more error: you don't write the record(s) to the file. The code only opens the file for writing (Rewrite() procedure).
  • _STONE__STONE_ Posts: 21Member
    : : : Hi!
    : : : I came across a stupid problem and I really have no idea why it arises. There is the following type declaration in my code:
    : : :
    : : : db_settings=record
    : : : default_db:boolean;
    : : : filename:string[100];
    : : : server:string[100];
    : : : db:string[100];
    : : : user:string[100];
    : : : password:string[100];
    : : : end;
    : : :
    : : : Then I write these things to a file:
    : : :
    : : : procedure TForm2.Button10Click(Sender: TObject);
    : : : var
    : : : db_sett:db_settings;
    : : : dbsfile:file of db_settings;
    : : : begin
    : : : assignfile(dbsfile,'db_settings.dat');
    : : : with db_sett do
    : : : begin
    : : : filename:=edit1.Text;
    : : : server:=edit2.Text;
    : : : db:=edit3.Text;
    : : : user:=Edit4.Text;
    : : : password:=Edit5.Text;
    : : : if radiobutton1.Checked then default_db:=true else default_db:=false;
    : : : rewrite(dbsfile);
    : : [red]closefile(dsbfile)[/red]
    : : : end;
    : : : end;
    : : :
    : : : The next code reads from that file:
    : : :
    : : : procedure TForm2.FormCreate(Sender: TObject);
    : : : var
    : : : db_sett:db_settings;
    : : : dbsfile:file of db_settings;
    : : : begin
    : : : if fileexists('db_settings.dat') then
    : : : begin
    : : : assignfile(dbsfile,'db_settings.dat');
    : : : reset(dbsfile);
    : : [red]While not EOF(dbsfile) do[/red]
    : : : read(dbsfile,db_sett);
    : : : with db_sett do
    : : : begin
    : : : Edit1.Text:=filename;
    : : : Edit2.Text:=server;
    : : : Edit3.Text:=db;
    : : : Edit4.Text:=user;
    : : : Edit5.Text:=password;
    : : : if default_db then radiobutton1.Checked:=true else radiobutton2.Checked:=true;
    : : : end;
    : : [red]closefile(dbsfile);[/red]
    : : : end;
    : : : end;
    : : :
    : : : And here is the problem - I get 'Read beyon end of file.' each time the procedure tries to read from the file.
    : : :
    : :
    : : I think you need to close the file and maybe use the EOF function to make sure that you are not trying to read beyond the end of the file.
    : : See your code in red. Zibadian might have a better answer for you.
    : :
    : : James
    : :
    : :
    : Nope. I don't have a better answer. This (including James's improvements) should work fine. I just found one more error: you don't write the record(s) to the file. The code only opens the file for writing (Rewrite() procedure).
    :
    Thanks! I have been looking at this code for more than 30 minutes and did'n see that I had'n wrote the record to the file!
Sign In or Register to comment.