Board index » delphi » Source Code Needs Sorting...

Source Code Needs Sorting...

Hi all,

I recently wrote a program for college, based on a system that would run a
shop similar to HMV, {*word*269} Megastores etc. I'd got quite far with it, but
it uses a serial access method of using the data in the systems files. My
teacher asked me to convert my program so that it uses random access, so
rather than convert my current system I took the original beta and converted
that...but it wont work!!! I've tried for weeks to get it to work...yes its
true, I'm still a novice. So I'm posting my source her for you guys (and
gals) to take a look at it, see if you can spot were I'm going wrong (if you
wouldn't mind),

Thanks in advance,

M.

Program Cd_Collection;

uses crt;

type
  cd=record
       artist:string[40];
       title: string[40];
       style:char;
       rating:integer;
       filepos:longint;
     end;

Var
  diskfile:file of cd;
  single:cd;
  counter:integer;
  choice:char;
  filepos:longint;

Procedure newcd;
Begin
  with single do
  begin
    write('Enter Artist: ');
    readln(artist);
    Write('Enter Title: ');
    readln(title);
    write('Enter Style (C)lassical, (J)azz, (R)ock, (F)olk: ');
    readln(style);
    write('Enter Rating (0-9)');
    readln(rating);
    filepos:=counter;
  end;
  seek(diskfile,filepos);
  write(diskfile,single);
  counter:=counter+1;
end;

Procedure Showone(disk : cd);
Begin
  If single.artist[1]<> chr(255) then
    with disk do
    Begin
      writeln('Record no. ',filepos);
      writeln(artist);
      writeln(title);
      Writeln(style);
    end;
end;

Procedure Display;
Var
  Loop:integer;
Begin
  for loop:= 1 to counter-1 do
  begin
     seek(diskfile,loop);
     read(diskfile,single);
     showone(single);
  end;
end;

Procedure Search;
Var
  loop:integer;
  target:string[40];
Begin
  Writeln('Enter Artist To Search For: ');
  readln(target);
  for loop:=1 to counter-1 do
  begin
     seek(diskfile,loop);
     read(diskfile,single);
    if single.artist=target
    then showone(single);
  end;
end;

Procedure Editrec;
Var
   Target:longint;
   tstring:string[40];
   tchar:char;

Begin
   write('Record Number? ');
   readln(target);
   if (target>0) and (target<counter)
      then Begin
             seek(diskfile,target);
             read(diskfile,single);
             showone(single);
             Write('Enter New Name or [ENTER] ');
             readln(tstring);
             if length(tstring)>0 then single.artist:=tstring;
             Write('Enter New Title or [ENTER] ');
             readln(tstring);
             if length(tstring)>0 then single.title:=tstring;
             Write('Enter New Style or [ENTER] ');
             readln(tchar);
             if tchar<>chr(13)
                then single.rating:=ord(tchar)-48;
             seek(diskfile,target);
             write(diskfile,single);
             End;
End;

Procedure fulldelete;
Var
  target:longint;

Begin
  write('Record Number?');
  readln(target);
  if (target>0) and (target<counter)
    then begin
       single.artist[1]:=chr(255);
       seek(diskfile,target);
       write(diskfile,single);
       end;
end;

Procedure closefile;
Begin
   single.filepos:=counter;
   seek(diskfile,0);
   write(diskfile,single);
   close(diskfile);
end;

Begin
  assign(diskfile,'randcoll.dat');
  rewrite(diskfile);
  counter:=1;
  reset(diskfile);
  read(diskfile,single);
  Counter:=single.filepos;
  writeln('Cd Collection Organiser');
repeat
  writeln('New Cd.................1');
  writeln('Display Collection.....2');
  writeln('Select By Artist.......3');
  writeln('Edit An Entry..........4');
  writeln('Delete Record..........5');
  Writeln('Exit Program...........6');
  readln(choice);
  case choice of
  '1':newcd;
  '2':display;
  '3':search;
  '4':editrec;
  '5':fulldelete;
  '6':closefile;
  end;
until choice='6';
Writeln ('Copywrite Carey Technological Design Systems 2000-2001');
Writeln ('Carey "Organiser" ver1.2');
Readln;
end.

 

Re:Source Code Needs Sorting...


Quote
On Tue, 9 Jan 2001 19:48:33 -0000, "M." <Huf...@Hotmail.Com> wrote:
>Hi all,

>I recently wrote a program for college, based on a system that would run a
>shop similar to HMV, {*word*269} Megastores etc. I'd got quite far with it, but
>it uses a serial access method of using the data in the systems files. My
>teacher asked me to convert my program so that it uses random access, so
>rather than convert my current system I took the original beta and converted
>that...but it wont work!!! I've tried for weeks to get it to work...yes its
>true, I'm still a novice. So I'm posting my source her for you guys (and
>gals) to take a look at it, see if you can spot were I'm going wrong (if you
>wouldn't mind),

>Thanks in advance,
>M.

..................... snip ..............................

On a very cursory examination, I notice there is some confusion
between a file of records and a file of bytes (for example). In a
record file the dimensions used in FilePos(f), FileSize(f) and
Seek(f, 5) are file numbers NOT BYTES. Another record file "oddity" is
that the first record number is 0.

Suppose you have 5 records in record file f. FileSize(f) = 5 but these
records number 0..4. If you should do Seek(f, 5), this will place you
at the end of record #4 where you can append a new record. (There is
no append command for a file of records.)

type
  cd=record
       artist:string[40];
       title: string[40];
       style:char;
       rating:integer;
       filepos:longint;     <----You do not need this,
                                 use filepos(diskfile)
     end;
.......................snip.......................

Begin
  assign(diskfile,'randcoll.dat');
  rewrite(diskfile); <----This will erase everything on the disk
  counter:=1;  ????

Unless you want to destroy previous info, just open with
reset(diskfile). You need not use rewrite in order to add or modify
a record. Just locate with seek and use Write(diskfile, single).

Other Threads