Delphi and Kylix

Moderators: pritaeas
Number of threads: 7264
Number of posts: 19073

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Reading from file Posted by _STONE_ on 22 Oct 2006 at 5:40 AM
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.
Report
Re: Reading from file Posted by jamesb800 on 22 Oct 2006 at 6:22 AM
: 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);
closefile(dsbfile)
: 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);
While not EOF(dbsfile) do
: 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;
closefile(dbsfile);
: 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

Report
Re: Reading from file Posted by zibadian on 22 Oct 2006 at 7:10 AM
: : 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);
: closefile(dsbfile)
: : 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);
: While not EOF(dbsfile) do
: : 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;
: closefile(dbsfile);
: : 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).
Report
Re: Reading from file Posted by _STONE_ on 22 Oct 2006 at 9:33 AM
: : : 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);
: : closefile(dsbfile)
: : : 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);
: : While not EOF(dbsfile) do
: : : 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;
: : closefile(dbsfile);
: : : 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!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.