waaromikniet
asked on
Beginner Delphi seeking help
I've got the follwoing unit:
************************** ********** ********** ********** ********** ********** ********** *****
unit input;
(************************* ********** ********** ********** ********** ********** *
Encapsulates TextFile for input
Example of use
--------------
FIN := TInputFile.Open(Filename); // create will set open flag
try
if FIN.IsOpen then begin
while not FIN.EOF do begin
S := FIN.NextLine;
{ process S }
if BadNews then showmessage('Error on '+FIN.LineOfName+' Text:"'+Fin.LastLine+'"');
end;
FIN.Close; // not necessary will be handled by FIN.free
end else begin
showmessage('Error:'+FIN.E msg);
end;
finally
FIN.free;
end;
************************** ********** ********** ********** ********** ********** ********)
interface
uses
SysUtils;
type
TInputFile = class(Tobject)
Private
F : Textfile;
fOpen : boolean;
Linecount : integer;
fname : string;
public
LastLine : string; // What was the last line of text provided by NextLine
EMsg: string; // Error message
constructor Open(const Filename:string);
destructor Destroy; override;
procedure Close; // when we finish
function EOF:boolean; // reached EOF?
Function isOpen : boolean; // open?
function LineOfName : string; // "Line n of file f" eg for reporting errors
function NextLine:string; // return next line of text
end;
implementation
constructor TInputFile.Open(const Filename:string);
{ create and open file.
isOpen function will tell if successfull
Emsg may give report of problem }
begin
inherited create;
fOpen := false;
LineCount := 0;
lastline := '';
if not fileexists(Filename) then begin
Emsg := 'File "'+Filename+ '" not found';
exit;
end;
try
assignfile(F,Filename);
reset(F);
fOpen := true;
fname := filename;
except
Emsg := 'Unable to open "'+Filename+ '"';
end;
end;
destructor TInputFile.Destroy;
{ make sure we have closed the file}
begin
if fopen then close;
inherited destroy;
end;
procedure TInputFile.Close;
{ close the file }
begin
try
closefile(F);
except
; // ignore any error
end;
end;
function TInputFile.EOF:boolean;
{ Are we at the EOF ? }
begin
result := system.eof(F);
end;
Function TInputFile.isOpen : boolean;
{ Is the file open }
begin
result := fOpen;
end;
function TInputFile.LineOfName : string;
{ return a text in the form of Line nnn of file fff}
begin
result := format('line %d of file "%s"',[LineCount,fname]);
end;
function TInputFile.NextLine:string ;
{ read next line }
begin
if eof then begin
result := '';
end else begin
readln(F,LastLine);
result := LastLine;
linecount := linecount+1;
end;
end;
end.
************************** ********** ********** ********** ********** ********** ********** *****
When lookong at the example of use i tried something like this:
************************** ********** ********** ********** ********** ********** ********** *****
unit mainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, inputFile;
type
Tmain = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
main: Tmain;
implementation
{$R *.dfm}
procedure Tmain.Button1Click(Sender: TObject);
var
S : string;
FIN : TInputFile;
const
filename = 'd:\temp\danny.txt';
begin
FIN.Open(filename); // create will set open flag
end;
Only when trying to create the TInputfile I get an acces violation.
Can anybody help me? And give a hint on how to use this inputreader/
**************************
unit input;
(*************************
Encapsulates TextFile for input
Example of use
--------------
FIN := TInputFile.Open(Filename);
try
if FIN.IsOpen then begin
while not FIN.EOF do begin
S := FIN.NextLine;
{ process S }
if BadNews then showmessage('Error on '+FIN.LineOfName+' Text:"'+Fin.LastLine+'"');
end;
FIN.Close; // not necessary will be handled by FIN.free
end else begin
showmessage('Error:'+FIN.E
end;
finally
FIN.free;
end;
**************************
interface
uses
SysUtils;
type
TInputFile = class(Tobject)
Private
F : Textfile;
fOpen : boolean;
Linecount : integer;
fname : string;
public
LastLine : string; // What was the last line of text provided by NextLine
EMsg: string; // Error message
constructor Open(const Filename:string);
destructor Destroy; override;
procedure Close; // when we finish
function EOF:boolean; // reached EOF?
Function isOpen : boolean; // open?
function LineOfName : string; // "Line n of file f" eg for reporting errors
function NextLine:string; // return next line of text
end;
implementation
constructor TInputFile.Open(const Filename:string);
{ create and open file.
isOpen function will tell if successfull
Emsg may give report of problem }
begin
inherited create;
fOpen := false;
LineCount := 0;
lastline := '';
if not fileexists(Filename) then begin
Emsg := 'File "'+Filename+ '" not found';
exit;
end;
try
assignfile(F,Filename);
reset(F);
fOpen := true;
fname := filename;
except
Emsg := 'Unable to open "'+Filename+ '"';
end;
end;
destructor TInputFile.Destroy;
{ make sure we have closed the file}
begin
if fopen then close;
inherited destroy;
end;
procedure TInputFile.Close;
{ close the file }
begin
try
closefile(F);
except
; // ignore any error
end;
end;
function TInputFile.EOF:boolean;
{ Are we at the EOF ? }
begin
result := system.eof(F);
end;
Function TInputFile.isOpen : boolean;
{ Is the file open }
begin
result := fOpen;
end;
function TInputFile.LineOfName : string;
{ return a text in the form of Line nnn of file fff}
begin
result := format('line %d of file "%s"',[LineCount,fname]);
end;
function TInputFile.NextLine:string
{ read next line }
begin
if eof then begin
result := '';
end else begin
readln(F,LastLine);
result := LastLine;
linecount := linecount+1;
end;
end;
end.
**************************
When lookong at the example of use i tried something like this:
**************************
unit mainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, inputFile;
type
Tmain = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
main: Tmain;
implementation
{$R *.dfm}
procedure Tmain.Button1Click(Sender:
var
S : string;
FIN : TInputFile;
const
filename = 'd:\temp\danny.txt';
begin
FIN.Open(filename); // create will set open flag
end;
Only when trying to create the TInputfile I get an acces violation.
Can anybody help me? And give a hint on how to use this inputreader/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
a bit too late ;-))
there is an example given at the top of the unit file. Have you checked that out ?
ASKER
When I do this I get the message
[Error] mainForm.pas(35): Undeclared identifier: 'FIN'
[Fatal Error] inputreader.dpr(6): Could not compile used unit 'mainForm.pas'
[Error] mainForm.pas(35): Undeclared identifier: 'FIN'
[Fatal Error] inputreader.dpr(6): Could not compile used unit 'mainForm.pas'
ASKER
Oops I erased this line
FIN : TInputFile;
This shoul be there am I right?
FIN : TInputFile;
This shoul be there am I right?
you are right, you need that line as the declaration.
ehm, thanks for the grade, but Imthiyaz_ph suggestion is the same and was first.
do you want to change the grade?
meikl ;-)
do you want to change the grade?
meikl ;-)
:))
Fin := TInputFile.Open(filename);