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.Emsg);
     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/



LVL 4
waaromiknietAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imthiyaz_phCommented:
you should use the constructor like this :

Fin := TInputFile.Open(filename);
0
kretzschmarCommented:
change this

begin
  FIN.Open(filename);    // create will set open flag
end;

into

begin
  FIN := TInputFile.Open(filename);    // create will set open flag
end;

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
a bit too late ;-))
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Imthiyaz_phCommented:
there is an example given at the top of the unit file. Have you checked that out ?

0
waaromiknietAuthor Commented:
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'
0
waaromiknietAuthor Commented:
Oops I erased this line

FIN : TInputFile;

This shoul be there am I right?
0
Imthiyaz_phCommented:
you are right, you need that line as the declaration.
0
kretzschmarCommented:
ehm, thanks for the grade, but Imthiyaz_ph suggestion is the same and was first.

do you want to change the grade?

meikl ;-)
0
Imthiyaz_phCommented:
:))
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.