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

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.