Board index » delphi » Novice trying to write to a file and then read from it

Novice trying to write to a file and then read from it

I am tring to write to a text file I have opened called EmployeeFile.
As my program is quite big I will not post it all, but can anyone give
any adice as to how I can get the Workers_Record procedure (seems to
work), to hold the data which has been entered and if another entry is
made, it writes this below instead of over the top of the previous
entry?

As you will see from the ReportHeader procedure, I also have a file
called OutputFile which hopefully one day will be a report which will
read data from the EmployeeFile and be written into the report.  I
have had to put Wage in {} brackets as the computer does not recognise
this...yet.

Any help or advice appreciated.

George

Procedure Workers_Record;      
var
        ValidCard, ValidTax, ValidNoShifts : Boolean;
        Wage : Real;

        begin
                writeln(EmployeeFile);
                writeln(EmployeeFile);

                writeln(EmployeeFile, WorkersName);
                writeln(EmployeeFile, ClockCode);

                ValidCard := ((ClockCode >= '1000') and  (ClockCode <'10000'));
                writeln(EmployeeFile, Shift);      

                writeln(EmployeeFile, TaxCode);                        
                ValidTax :=  ((TaxCode[1]>='0') and (TaxCode[1]<='9'))
                        and ((TaxCode[2]>='0') and (TaxCode[2]<='9'))
                        and ((TaxCode[3]>='0') and (TaxCode[3]<='9'))
                        and ((TaxCode[4]='l') or (TaxCode[4]='h')
                                or (TaxCode[4]='L') or (TaxCode[4]='H'));

                writeln(EmployeeFile, Shift);

                writeln(EmployeeFile, ShiftsWorked);    
                ValidNoShifts := (ShiftsWorked >= 1) and (ShiftsWorked <= 7);

                writeln(EmployeeFile, AddHours);
                        begin
                                if ((Shift = 'a') or (shift = 'A')) then
                                        Wage := ((AttendPay_A * ShiftsWorked) +
(AddHoursPay_A * AddHours))
                                else
                                        Wage := ((AttendPay_B * ShiftsWorked) +
(AddHoursPay_B * AddHours));
                        end;
                writeln(EmployeeFile, Wage);
        end;

Procedure ReportHeader;  {Header to display details at the top of the
reoprt}
        begin
                reset (EmployeeFile);
                while not eof(OutputFile) do;
                begin
                writeln(OutputFile);
                writeln(OutputFile);
                writeln(OutputFile, CompanyName:10, 'Week No. ':45, WeekNo);
                writeln(OutputFile, 'Date ':55, Days, '/', Month, '/', Year);
                writeln(OutputFile);
                writeln(OutputFile);

                        begin
                        repeat
                                readln(EmployeeFile, WorkersName,
ClockCode, TaxCode, Shift, AddHours, {Wage}));
                                writeln(OutputFile, WorkersName:20,
ClockCode:4, TaxCode:4, Shift:1, AddHours:1, {Wage:6:2});
                        until eof(OutputFile);
                        end;
                end;
        end;

 

Re:Novice trying to write to a file and then read from it


Quote
> writeln(EmployeeFile);

Will definitly not work
If you want to read something from a file, you should use readln, not
writeln

example:

var myfile: text;
     mystring: string;

begin
  assign(myfile, 'c:\autoexec.bat')
  reset(myfile);
  readln(myfile,mystring);
  writeln(mystring);
  close(myfile)
end.

I would also suggest that you use a binary file instead of a typed one

Greetings,
YVII

"George J" <georgesm...@freeze.com> schreef in bericht
news:8afeecdf.0202091434.67b8143c@posting.google.com...

Quote
> I am tring to write to a text file I have opened called EmployeeFile.
> As my program is quite big I will not post it all, but can anyone give
> any adice as to how I can get the Workers_Record procedure (seems to
> work), to hold the data which has been entered and if another entry is
> made, it writes this below instead of over the top of the previous
> entry?

> As you will see from the ReportHeader procedure, I also have a file
> called OutputFile which hopefully one day will be a report which will
> read data from the EmployeeFile and be written into the report.  I
> have had to put Wage in {} brackets as the computer does not recognise
> this...yet.

> Any help or advice appreciated.

> George

> Procedure Workers_Record;
> var
> ValidCard, ValidTax, ValidNoShifts : Boolean;
> Wage : Real;

> begin
> writeln(EmployeeFile);
> writeln(EmployeeFile);

> writeln(EmployeeFile, WorkersName);
> writeln(EmployeeFile, ClockCode);

> ValidCard := ((ClockCode >= '1000') and  (ClockCode <'10000'));
> writeln(EmployeeFile, Shift);

> writeln(EmployeeFile, TaxCode);
> ValidTax :=  ((TaxCode[1]>='0') and (TaxCode[1]<='9'))
> and ((TaxCode[2]>='0') and (TaxCode[2]<='9'))
> and ((TaxCode[3]>='0') and (TaxCode[3]<='9'))
> and ((TaxCode[4]='l') or (TaxCode[4]='h')
> or (TaxCode[4]='L') or (TaxCode[4]='H'));

> writeln(EmployeeFile, Shift);

> writeln(EmployeeFile, ShiftsWorked);
> ValidNoShifts := (ShiftsWorked >= 1) and (ShiftsWorked <= 7);

> writeln(EmployeeFile, AddHours);
>                         begin
>                 if ((Shift = 'a') or (shift = 'A')) then
>                 Wage := ((AttendPay_A * ShiftsWorked) +
> (AddHoursPay_A * AddHours))
>                 else
>                 Wage := ((AttendPay_B * ShiftsWorked) +
> (AddHoursPay_B * AddHours));
>                         end;
> writeln(EmployeeFile, Wage);
> end;

> Procedure ReportHeader;  {Header to display details at the top of the
> reoprt}
>         begin
>                 reset (EmployeeFile);
>                 while not eof(OutputFile) do;
>                 begin
> writeln(OutputFile);
> writeln(OutputFile);
> writeln(OutputFile, CompanyName:10, 'Week No. ':45, WeekNo);
> writeln(OutputFile, 'Date ':55, Days, '/', Month, '/', Year);
> writeln(OutputFile);
>                 writeln(OutputFile);

>                         begin
>                         repeat
>                                 readln(EmployeeFile, WorkersName,
> ClockCode, TaxCode, Shift, AddHours, {Wage}));
>                                 writeln(OutputFile, WorkersName:20,
> ClockCode:4, TaxCode:4, Shift:1, AddHours:1, {Wage:6:2});
>                         until eof(OutputFile);
>                         end;
>                 end;
>         end;

Re:Novice trying to write to a file and then read from it


On 9 Feb 2002 14:34:23 -0800, georgesm...@freeze.com (George J) wrote:

Quote
>I am tring to write to a text file I have opened called EmployeeFile.
>As my program is quite big I will not post it all, but can anyone give
>any adice as to how I can get the Workers_Record procedure (seems to
>work), to hold the data which has been entered and if another entry is
>made, it writes this below instead of over the top of the previous
>entry?

...................snip..............>

Program Record_Demo;
{This is a suggestion to make your data easier to handle. It appears
that the format of your data could be a Pascal record and stored
for random access.

This is a primitive demo of writing and reading records to and from
disk using typed records. Record data can be almost any type
including a user defined type. Data on disk can be used with the SEEK
command for access of any record number beginning with record# 0 !!
When a filename is entered: (1) if it does not exist, it is created;
(2) if it does exist, the new records are appended to the file
instead of overwriting. All records take the same disk space on disk
so any field can be updated in place when needed, such as a change of
wages. In this demo no provision is made to delete the datafile.

The example here simulates registering a student with an ID number.
maximum students are 10 (or whatever you like).  Turbo v6.0}

CONST
fname = 'Register.dat';  {this is created if missing}
maxStudents = 10;
TYPE
student = RECORD
             Name : String[25];
             ID : LongInt;
          End;
Registered = File of student;  {single word TYPE needed for
parameters}

VAR
single:student;
f:Registered;   {random access record file}
ct, total:Integer;

Procedure OpenFile(VAR fParm:Registered; VAR tot:Integer);
{ If file REGISTER.DAT already exists, new data will be appended to
the existing file. The Pascal 'APPEND' only works with text files so a
different method must be used. If the file does not exist, it will be
created.  }

Begin
     {$I-}    (* Removes IO error abort if file missing *)
     Assign(fParm, fname);
     Reset(fParm);
     {$I+}    (* Replace IO error abort *)
     If IOResult = 0 then  (* File exists *)
          SEEK(fParm, FILESIZE(fParm)) (* now at end of last record *)
     Else Rewrite(fParm);  (* create file *)
     tot := FILESIZE(fParm);
End;

Procedure EnterStudent(VAR entry:student; VAR finis:Boolean);
Begin
     finis := false;
     Writeln; Write('student name: ');
     Readln(entry.Name);
     If Length(entry.Name) > 0 then
     Begin
          Write('ID number: ');
          Readln(entry.ID);
     End
     Else finis := True;
End;

Procedure StoreStudents(VAR fParm:Registered; VAR tot:Integer);
VAR done:Boolean;
    ent:student;
Begin
     Writeln; Write('Enter all students. When finished, just press ');
              Writeln('<Enter> instead of student name.');
     Repeat
           If tot < MaxStudents Then EnterStudent(ent, done);
           If not done then
           Begin
                Inc(tot);
                Write(fParm, ent);  {sequential}
           End;
     Until (done) or (tot = MaxStudents);
End;

BEGIN
     Writeln; Writeln;
     OpenFile(f, total);
     StoreStudents(f, total);

{Showing random access by reading in reverse order}
      For ct := total-1 downto 0 Do
      Begin
          Seek(f, ct);
          Read(f, single);
          Writeln(single.Name:25, '  ', single.ID);
      End;
Close(f);
readln;
END.

Re:Novice trying to write to a file and then read from it


If you will send me an email with you correct address, I have some
more on your problem that may be of interest but requires 2 files to
describe.
Regards,
Clif  <clifp...@airmail.net

Re:Novice trying to write to a file and then read from it


Re:Novice trying to write to a file and then read from it


Sorry about the bad address. Due to spamming, the site decided only
those willing to pay would get their e-mail facility.
I have managed to write to the file ok, but reading from it seems to
be a bit of a problem now. Any suggestions are appreciated.

Thanks
George

Tried to send this to your e-mail address, but it didn't deliver - see
below.

Read Mail
--------------------------------------------------------------------------------

From: Mail Delivery Subsystem <MAILER-DAE...@mta1-3.us4.outblaze.com>
[Save Address] [Block Sender]
To: <georgesm...@mail.com>
Cc:  
Subject:  Returned mail: see transcript for details
Date:  Wed, 13 Feb 2002 21:15:00 GMT

REPLY | REPLY ALL | FORWARD [As Attachment]  Previous | Next
               Drafts Holding SENT Trash    

--------------------------------------------------------------------------------

The original message was received at Wed, 13 Feb 2002 21:15:00 GMT
from 205-158-62-51.outblaze.com [205.158.62.51]
----- The following addresses had permanent fatal errors -----
<clifp...@airmail.net>
(reason: 550 mail from 205.158.62.44 rejected: administrative
prohibition)

----- Transcript of session follows -----
... while talking to mx2.airmail.net.:

Quote
>>> RCPT To:<clifp...@airmail.net>

<<< 550 mail from 205.158.62.44 rejected: administrative prohibition
550 5.1.1 <clifp...@airmail.net>... User unknown
Quote
Clif Penn <clifp...@airmail.net> wrote in message <news:F10AD5D6882690BD.6DB6E0EA5BC0BE50.DD869913BE8D741A@lp.airnews.net>...
> If you will send me an email with you correct address, I have some
> more on your problem that may be of interest but requires 2 files to
> describe.
> Regards,
> Clif  <clifp...@airmail.net

Other Threads