?
Solved

Beginner Delphi seeking help

Posted on 2004-11-30
9
Medium Priority
?
243 Views
Last Modified: 2010-04-05
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/



0
Comment
Question by:waaromikniet
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:Imthiyaz_ph
ID: 12703744
you should use the constructor like this :

Fin := TInputFile.Open(filename);
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 600 total points
ID: 12703745
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12703751
a bit too late ;-))
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:Imthiyaz_ph
ID: 12703778
there is an example given at the top of the unit file. Have you checked that out ?

0
 
LVL 4

Author Comment

by:waaromikniet
ID: 12703789
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
 
LVL 4

Author Comment

by:waaromikniet
ID: 12703808
Oops I erased this line

FIN : TInputFile;

This shoul be there am I right?
0
 
LVL 6

Expert Comment

by:Imthiyaz_ph
ID: 12703832
you are right, you need that line as the declaration.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12704018
ehm, thanks for the grade, but Imthiyaz_ph suggestion is the same and was first.

do you want to change the grade?

meikl ;-)
0
 
LVL 6

Expert Comment

by:Imthiyaz_ph
ID: 12704103
:))
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question