Dear All,

Could you tell me how can I read from the hotseat.txt file all the records and sort by first name. Finally, list the sorted results on the screen.

Cheers,

turbomen

Recommended Answers

All 2 Replies

Program SortFile;
Var
    Hotseat,Tmp1,Tmp2,Final:Text;
    Buffer1,Buffer2: string[52];
    FirstName,Name1,Name2:String[12];
    LastTmp1,LastTmp2:String[12];
begin
    assign(Hotseat,'Hotseat.TXT');
    assign(Tmp1,'Less.TXT');
    assign(Tmp2,'Grade.TXT');
    assign(Final,'Final.TXT');
    Reset(Hotseat);
    ReWrite(Tmp1);
    ReWrite(Tmp2);
    Read(HotSeat,Buffer1);
    FirstName:=Buffer1;
    Writeln(Tmp1,Buffer1);
    LastTmp1:=FirstName;
    While Not Eof(HotSeat) do
    Begin
        Read(HotSeat,Buffer1);
        FirstName:=Buffer1;
        if FirstName>=LastTmp1 then
        Begin
            Writeln(Tmp1,Buffer1);
            LastTmp1:=FirstName;
        End
        Else
        Begin
            Writeln(Tmp2,Buffer1);
        End
    End;
    Close(Tmp1);
    Close(Tmp2);
    Reset(Tmp1);
    Reset(Tmp2);
    ReWrite(Final);
    Read(Tmp1,Buffer1);
    Name1:=Buffer1;
    Read(Tmp2,Buffer2);
    Name2:=Buffer2;
    While Not((Eof(Tmp1)) Or (Eof(Tmp2))) do
    Begin
        If Name1>Name2 Then
        Begin
            WriteLn(Final,Buffer2);
            Readln(Tmp2,Buffer2);
            Name2:=Buffer2;
        End
        Else
        Begin
            WriteLn(Final,Buffer1);
            Readln(Tmp1,Buffer1);
            Name1:=Buffer1;
        End;
    End;
    While Not Eof(Tmp1) Do
    Begin
        Readln(Tmp1,Buffer1);
        WriteLn(Final,Buffer1);
    End;
    While Not Eof(Tmp2) Do
    Begin
        Readln(Tmp2,Buffer2);
        WriteLn(Final,Buffer2);
    End;
    Close(Final);
    Reset(Final);
    While Not Eof(Final) Do
    Begin
        Readln(Final,Buffer1);
        WriteLn(Buffer1);
    End;
    Close(Final);
    ReadLn;
end.
{
First I created a new file,cause yours is not proper to me,sorry
And I write 5 members into it,and then read them into an array and
last short them
by the way,there are lot of shorting algorithm,I show you one of them

simple sample:
(*pascal but it is working on delphi 7 too*)
program algorithm;
const n = 6;
var i,j,k,a:integer;
    b:array[1..n]of integer;
begin
        b[1]:=5;
        b[2]:=9;
        b[3]:=2;
        b[4]:=3;
        b[5]:=7;
        b[6]:=1;

        for i:=1 to n-1 do begin

                for j:= i+1 to n do begin

                        if (b[i]>b[j]) then begin
                                a:=b[i];
                                b[i]:=b[j];
                                b[j]:=a;
                        end;
                end;
        end;

        (*result*)
        for k:=1 to n do begin
                writeln(b[k]);
        end;
        readln;
end.
}


(*This is an example for your question,created in delphi 7*)

program arrange_array;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type tr = record
             f:string[20];
             l:string[20];
          end;


var temp:tr;  {this will store one record temporary}
    allin,allout:array[1..5]of tr;{every elements is one record}
    i,j,k,p,x:integer;{these are loop vars}
    myfile:file of tr;  {need a file var too}
    myfilepath:string;{stores the file's path}

begin
   {let's fill the array with some old stars,when i was young...}

   allout[1].f:='turbo';{first name}
   allout[1].l:='b';{last name}
   allout[2].f:='mc';
   allout[2].l:='hammer';
   allout[3].f:='joey b';
   allout[3].l:='elis';
   allout[4].f:='bg the price';
   allout[4].l:='of rap';
   allout[5].f:='naughty by';
   allout[5].l:='nature';

   {let's add the file path and its name and its extension}
   myfilepath:='C:\names.dat';
   {assign the file to the file var}
   assignfile(myfile,myfilepath);
   {create it,if it is exists then delete its contents...}
   rewrite(myfile);

   writeln('Unshorted array: ');

   {let's write the above array to this file}
   reset(myfile);
   for i:=1 to 5 do begin
        write(myfile,allout[i]);
        writeln(i,'. ',allout[i].f,' ',allout[i].l);
   end;
   {closing...}
   close(myfile);
   {open again,now to read into an array}
   {$I-}
   reset(myfile);
   {$I+}
   if (ioresult <> 0) then begin
        writeln('Error: ',ioresult); {write the error code,just a number...}
        readln;
        exit;
   end;
   {read into allin}
   j:=1;
   while not eof(myfile)do begin
        read(myfile,allin[j]);
        inc(j,1);
   end;
   close(myfile);

   writeln;
   writeln;
   {short them,by first name}
   for k:=1 to 4 do begin
        for p:=k+1 to 5 do begin
                if ( allin[k].f > allin[p].f ) then begin
                        temp:=allin[k];
                        allin[k]:=allin[p];
                        allin[p]:=temp;
                end;
        end;
   end;
   {write out the results...}
   writeln('Shorted array: ');
   for x:=1 to 5 do begin
        writeln(x,'. ',allin[x].f,' ',allin[x].l);
   end;

   readln;
end.
{by FlamingClaw 2009.09.}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.