Solved

creating a file

Posted on 1998-02-18
8
198 Views
Last Modified: 2010-04-16
could you please help me

Hi I am Tim Boon a student studying computers I am writting a program for my own use, of
which I have programs to build, append, inspect and search the database, however I am
having a bit of trouble writing a program to delete amd edit the records in the data file.
Below is my program to search the data file.  I have got to algorithms for deleting and
editing and I would much appreciate them being written into pascal.

thankyou very much for the help

TimBoon@netcomuk.co.ukj


HERE IS AN ALGORITHM TO DELETE A RECORD FROM A DATA FILE (USE SEARCH FILE AS A BASE)




set found flag to false
input key field of record to be dleted into the search field

{open file}
open original file for reading
open temporary file for reading

{copy contents of original file to temporary file excluding deletd record}
while not end of original file do
read record from the original field
If key field does not equal search field
then
write record to temporary field
end if
if key field equals search field
then
set found flag to true
end if
end while

{re open files}
open temporary file for reading
open original file for reading

{copy contents of original file overwriting the contents of the original file}
while not end of temporary file do
read record from the temporary file
write record to the original file
end while

if not found flag then display error message 'record not found'

flush output buffer to original file  

   



HERE IS AN ALGORITHM TO EDIT THE CONTENTS OF A DATA FILE (USE SEARCH FILE AS A BASE)



set found flag to false
input key field of record to be dleted into the search field

{open file}
open original file for reading
open temporary file for reading

{COPY CONTENTS OF ORIGINAL TEMPORARY FILE EDITING CONTENTS OF SELECTED RECORDS BEFORE COPYING IT}
while not end of original file do
read record from the original file
if key field equals search field
then
set found flag to true
repeat
display headings
display all fields on screen
change selected fields
until no more changes
end if
write record to temporary file
end while

{re open files}
open temporary file for reading
open original file for reading

{copy contents of original file overwriting the contents of the original file}
while not end of temporary file do
read record from the temporary file
write record to the original file
end while

if not found flag then display error message 'record not found'

flush output buffer to original file  





USE THE FOLLOWING PROGRAM AS A BASIS FOR THE TWO PROGRAMS ABOVE FILE AS A BASE)
USE THIS PROGRAM TO DELETE AND EDIT DATA FILES AND THIS IS WHAT ALL MY DATA FILES ARE CALLED
ETC.


ROGRAM SEARCH(OUTPUT, sail);
{program to search sail records}
{Tim Boon 10th Febuary 1998}

TYPE
  CalenderDetails=   RECORD
                  Date             :STRING;  {Date of event}
                  Event            :STRING;  {What event is}
                  Venue            :STRING;  {Wher being held}
                  Contact          :STRING;  {Who to contact}
                    Telephone        :INTEGER; {Contacts telephone}
                  Price            :INTEGER; {Price for event}
                  SeaInland        :STRING;  {If at sea or inland}
                  Accommodation    :STRING;  {If accomadion at venue}
                  Social           :STRING;  {What socil there is}
                  EntryReq         :STRING;  {To sail at event}
                  Directions       :STRING;  {Direction to the event}
                  Food             :STRING;  {What food is availible}
                  MaxNumber        :INTEGER; {Max number of entries}
                  Age              :INTEGER  {If there is an age limit}

              END;


  SailFile = FILE OF CalenderDetails;

VAR
  found                      :boolean;
  SailRecord                 :CalenderDetails;
  sail                       :sailFile;
  SearchDate                  :STRING;
BEGIN
     ASSIGN(sail,'a:calend.dat');
     Found:=false;
     Write('What date are you looking for-');
     ReadLn(SearchDate);


     RESET(sail);

     WriteLn(' Contents of Sail File'); WriteLn;

     WHILE (NOT EOF(sail)) and (NOT FOUND) DO
        BEGIN
           WITH SailRecord DO
              BEGIN

               Read(sail, SailRecord);
              IF SearchDate=Date Then
              BEGIN
              WriteLn('Date                       : ',Date);
              WriteLn('Event                      : ',Event);
              WriteLn('Venue                      : ',Venue);
              WriteLn('Contact                    : ',Contact);
              WriteLn('Teephone                   : ',Telephone);
              WriteLn('Price                      : ',Price);
              WriteLn('SeaInland                  : ',SeaInland);
              WriteLn('Accommodation               : ',Accommodation  );
              WriteLn('Social                     : ',Social);
              WriteLn('EntryReq                   : ',EntryReq);
              WriteLn('Directions                 : ',Directions);
              WriteLn('Food                       : ',Food);
              WriteLn('MaxNumber                  : ',MaxNumber);

             END; {IF;}
        END;
        {end with}
  END;
  {end while}
  WriteLn;
  If NOT Found THEN WriteLn('Record not found');
  ReadLn;
  CLOSE(sail);
END. {inspect}

                                                             
thankyou very much for helping me


TimBoon@netcomuk.co.ukj


0
Comment
Question by:tboon
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Expert Comment

by:zircon
ID: 1217353
In my opinion, the amount of code you are requesting would seem to be worth more than 100 points based on the amount of time needed to write it.

Do you have a question or questions about how to code specific portions of the delete and edit algorithms?
0
 

Expert Comment

by:bslim
ID: 1217354
Umm..I might be wrong ....but at the algorithm in which u are deleting records, how come both of the files are on read mode? Aren't u reading from one and writing to another? I saw some write statements over there but I don't see a file opened to write..
Hopefully this helps...if not i'll try again
:)

0
 

Expert Comment

by:bslim
ID: 1217355
Sorry...my apologies...I didn't notice the sentance
Please make a program from an algorithm...heheh..sorry bout that....

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:tboon
ID: 1217356
Adjusted points to 200
0
 
LVL 1

Accepted Solution

by:
zircon earned 200 total points
ID: 1217357
Tim,

Here is the answer for the delete program.  I will try to post the edit program as a comment as soon as I finish writing it.  Please leave this question locked until I've had a chance to post the code for the edit program.

I followed your algorithm for the delete program for the most part.  The only exception is that the source code I provide below does not rewrite the original file (calend.dat) if the record is not found.

I compiled and tested the program before I posted it (and it works), so I hope it is what you are looking for.  I'll try to post the edit program within the next 24 hours.  Good luck.

Bruce

program delete;
{This program deletes a record from a file based on user input}

type
    CalendarDetails = record
         Date: string;
         Event: string;
         Venue: string;
         Contact: string;
         Telephone: integer;
         Price: integer;
         SeaInland: string;
         Accomodation: string;
         Social: string;
         EntryReq: string;
         Directions: string;
         Food: string;
         MaxNumber: integer;
         Age: integer;
    end;

    SailFile = file of CalendarDetails;

var found: boolean;
    SailRecord: CalendarDetails;
    sail, temp: SailFile;
    SearchDate: string;

begin
    found := false;
    write ('What date would you like to delete - ');
    readln (SearchDate);

    assign (sail, 'a:\calend.dat');
    assign (temp, 'a:\temp.dat');
    reset (sail);
    rewrite (temp);

    while not eof (sail) do
         begin
              with SailRecord do
                   begin
                        read (sail, SailRecord);

                        if SearchDate <> Date then
                             write (temp, SailRecord)
                        else
                             found := true;
                   end; {with}

         end; {while}


    if not found then
         writeln ('Record not found.')
    else
         begin
              reset (temp);
              rewrite (sail);

              while not eof (temp) do
                   begin
                        read (temp, SailRecord);
                        write (sail, SailRecord);
                   end; {while}

         end; {else}

    close (temp);
    close (sail);

end. {main}
0
 
LVL 1

Expert Comment

by:zircon
ID: 1217358
Tim,

Here is the source code for the editing program.  Again, the program only rewrites the original file if the searched date was found.  As before, I tested this code and it works.

Thanks for increasing the points.  Again, I hope this helps and is what you were looking for.

Bruce

program editfile;
{This program edits a record from a file based on user input}

uses crt;

type
    CalendarDetails = record
         Date: string;
         Event: string;
         Venue: string;
         Contact: string;
         Telephone: integer;
         Price: integer;
         SeaInland: string;
         Accomodation: string;
         Social: string;
         EntryReq: string;
         Directions: string;
         Food: string;
         MaxNumber: integer;
         Age: integer;
    end;

    SailFile = file of CalendarDetails;

var found: boolean;
    SailRecord: CalendarDetails;
    sail, temp: SailFile;
    SearchDate: string;
    SelField: integer;

begin
    clrscr;
    found := false;
    write ('What date would you like to edit - ');
    readln (SearchDate);

    assign (sail, 'a:\calend.dat');
    assign (temp, 'a:\temp.dat');
    reset (sail);
    rewrite (temp);

    while not eof (sail) do
      begin
        with SailRecord do
          begin
            read (sail, SailRecord);

            if SearchDate <> Date then
              write (temp, SailRecord)
            else
              begin
                found := true;

                repeat
                {The menu below can placed outside the repeat
                 loop to make program more efficient if desired,                  but then the menu may scroll off the screen                  during execution}
                  writeln ('1  Date: ', Date);
                  writeln ('2  Event: ', Event);
                  writeln ('3  Venue: ', Venue);
                  writeln ('4  Contact: ', Contact);
                  writeln ('5  Telephone: ', Telephone);
                  writeln ('6  Price: ', Price);
                  writeln ('7  SeaInland: ', SeaInland);
                  writeln ('8  Accomodation: ', Accomodation);
                  writeln ('9  Social: ', Social);
                  writeln ('10 EntryReq: ', EntryReq);
                  writeln ('11 Directions: ', Directions);
                  writeln ('12 Food: ', Food);
                  writeln ('13 MaxNumber: ', MaxNumber);
                  writeln ('14 Age: ', Age);
        write ('Select number of field to edit or -1 to exit: ');
                  readln (SelField);

                  case SelField of
                    -1 : writeln ('Editing completed.');
                    1  : begin
                           write ('Enter revised Date: ');
                           readln (Date);
                         end;
                    2  : begin
                           write ('Enter revised Event: ');
                           readln (Event);
                         end;
                    3  : begin
                           write ('Enter revised Venue: ');
                           readln (Venue);
                         end;
                    4  : begin
                           write ('Enter revised Contact: ');
                           readln (Contact);
                         end;
                    5  : begin
                          write ('Enter revised Phone number: ');
                          readln (Telephone);
                         end;
                    6  : begin
                           write ('Enter revised Price: ');
                           readln (Price);
                         end;
                    7  : begin
                           write ('Enter revised SeaInland: ');
                           readln (SeaInland);
                         end;
                    8  : begin
                          write ('Enter revised Accomodation: ');
                          readln (Accomodation);
                         end;
                    9  : begin
                           write ('Enter revised Social: ');
                           readln (Social);
                         end;
                    10 : begin
                           write ('Enter revised EntryReq: ');
                           readln (EntryReq);
                         end;
                    11 : begin
                           write ('Enter revised Directions: ');
                           readln (Directions);
                         end;
                    12 : begin
                           write ('Enter revised Food: ');
                           readln (Food);
                         end;
                    13 : begin
                           write ('Enter revised MaxNumber: ');
                           readln (MaxNumber);
                         end;
                    14 : begin
                           write ('Enter revised Age: ');
                           readln (Age);
                         end;
                    else
                     writeln (chr(7),
                              'ERROR: Invalid menu selection.')
                  end; {case}
                  writeln;
                until SelField = -1;

                write (temp, SailRecord);
              end; {else}

        end; {with}

    end; {while}


    if not found then
         writeln ('Record not found.')
    else
         begin
              reset (temp);
              rewrite (sail);

              while not eof (temp) do
                   begin
                        read (temp, SailRecord);
                        write (sail, SailRecord);
                   end; {while}

         end; {else}

    close (temp);
    close (sail);

end. {main}
0
 

Author Comment

by:tboon
ID: 1217359
Thanks for doing the program it is really cool, and I think that I understand most of it thankyou very much, this site is really great and just kep up the good work.  thanks again Tim Boon
0
 
LVL 1

Expert Comment

by:zircon
ID: 1217360
You're welcome.

These record processing programs (edit, delete, etc.) would actually a bit more efficient if you were to use an array of records to store the information during editing and deleting rather than a temporary file on a floppy disk.  That is unless memory is a major consideration.  If you would like, we can continue that discussion via e-mail.  I've got your e-mail address, so I'll send you a message later.

Bruce
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Ascii code 2 989
Delphi TClientDataSet - override Refresh event 4 765
Delphi TSQL Connection runtime creating 1 1,093
Secure Boot on W8 & 8.1 8 46
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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