943,636 Members | Top Members by Rank

Ad:
Sep 21st, 2009
-1

How can I list the sorted results on the screen?

Expand Post »
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
Attached Files
File Type: txt hotSeat.txt (1.6 KB, 32 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Sep 24th, 2009
1

Re: How can I list the sorted results on the screen?

Pascal and Delphi Syntax (Toggle Plain Text)
  1. Program SortFile;
  2. Var
  3. Hotseat,Tmp1,Tmp2,Final:Text;
  4. Buffer1,Buffer2: string[52];
  5. FirstName,Name1,Name2:String[12];
  6. LastTmp1,LastTmp2:String[12];
  7. begin
  8. assign(Hotseat,'Hotseat.TXT');
  9. assign(Tmp1,'Less.TXT');
  10. assign(Tmp2,'Grade.TXT');
  11. assign(Final,'Final.TXT');
  12. Reset(Hotseat);
  13. ReWrite(Tmp1);
  14. ReWrite(Tmp2);
  15. Read(HotSeat,Buffer1);
  16. FirstName:=Buffer1;
  17. Writeln(Tmp1,Buffer1);
  18. LastTmp1:=FirstName;
  19. While Not Eof(HotSeat) do
  20. Begin
  21. Read(HotSeat,Buffer1);
  22. FirstName:=Buffer1;
  23. if FirstName>=LastTmp1 then
  24. Begin
  25. Writeln(Tmp1,Buffer1);
  26. LastTmp1:=FirstName;
  27. End
  28. Else
  29. Begin
  30. Writeln(Tmp2,Buffer1);
  31. End
  32. End;
  33. Close(Tmp1);
  34. Close(Tmp2);
  35. Reset(Tmp1);
  36. Reset(Tmp2);
  37. ReWrite(Final);
  38. Read(Tmp1,Buffer1);
  39. Name1:=Buffer1;
  40. Read(Tmp2,Buffer2);
  41. Name2:=Buffer2;
  42. While Not((Eof(Tmp1)) Or (Eof(Tmp2))) do
  43. Begin
  44. If Name1>Name2 Then
  45. Begin
  46. WriteLn(Final,Buffer2);
  47. Readln(Tmp2,Buffer2);
  48. Name2:=Buffer2;
  49. End
  50. Else
  51. Begin
  52. WriteLn(Final,Buffer1);
  53. Readln(Tmp1,Buffer1);
  54. Name1:=Buffer1;
  55. End;
  56. End;
  57. While Not Eof(Tmp1) Do
  58. Begin
  59. Readln(Tmp1,Buffer1);
  60. WriteLn(Final,Buffer1);
  61. End;
  62. While Not Eof(Tmp2) Do
  63. Begin
  64. Readln(Tmp2,Buffer2);
  65. WriteLn(Final,Buffer2);
  66. End;
  67. Close(Final);
  68. Reset(Final);
  69. While Not Eof(Final) Do
  70. Begin
  71. Readln(Final,Buffer1);
  72. WriteLn(Buffer1);
  73. End;
  74. Close(Final);
  75. ReadLn;
  76. end.
Reputation Points: 12
Solved Threads: 10
Junior Poster in Training
fayyaz is offline Offline
66 posts
since Jun 2007
Sep 28th, 2009
1

Re: How can I list the sorted results on the screen?

delphi Syntax (Toggle Plain Text)
  1.  
  2. {
  3. First I created a new file,cause yours is not proper to me,sorry
  4. And I write 5 members into it,and then read them into an array and
  5. last short them
  6. by the way,there are lot of shorting algorithm,I show you one of them
  7.  
  8. simple sample:
  9. (*pascal but it is working on delphi 7 too*)
  10. program algorithm;
  11. const n = 6;
  12. var i,j,k,a:integer;
  13.   b:array[1..n]of integer;
  14. begin
  15.   b[1]:=5;
  16.   b[2]:=9;
  17.   b[3]:=2;
  18.   b[4]:=3;
  19.   b[5]:=7;
  20.   b[6]:=1;
  21.  
  22.   for i:=1 to n-1 do begin
  23.  
  24.   for j:= i+1 to n do begin
  25.  
  26.   if (b[i]>b[j]) then begin
  27.   a:=b[i];
  28.   b[i]:=b[j];
  29.   b[j]:=a;
  30.   end;
  31.   end;
  32.   end;
  33.  
  34.   (*result*)
  35.   for k:=1 to n do begin
  36.   writeln(b[k]);
  37.   end;
  38.   readln;
  39. end.
  40. }
  41.  
  42.  
  43. (*This is an example for your question,created in delphi 7*)
  44.  
  45. program arrange_array;
  46.  
  47. {$APPTYPE CONSOLE}
  48.  
  49. uses
  50. SysUtils;
  51.  
  52. type tr = record
  53. f:string[20];
  54. l:string[20];
  55. end;
  56.  
  57.  
  58. var temp:tr; {this will store one record temporary}
  59. allin,allout:array[1..5]of tr;{every elements is one record}
  60. i,j,k,p,x:integer;{these are loop vars}
  61. myfile:file of tr; {need a file var too}
  62. myfilepath:string;{stores the file's path}
  63.  
  64. begin
  65. {let's fill the array with some old stars,when i was young...}
  66.  
  67. allout[1].f:='turbo';{first name}
  68. allout[1].l:='b';{last name}
  69. allout[2].f:='mc';
  70. allout[2].l:='hammer';
  71. allout[3].f:='joey b';
  72. allout[3].l:='elis';
  73. allout[4].f:='bg the price';
  74. allout[4].l:='of rap';
  75. allout[5].f:='naughty by';
  76. allout[5].l:='nature';
  77.  
  78. {let's add the file path and its name and its extension}
  79. myfilepath:='C:\names.dat';
  80. {assign the file to the file var}
  81. assignfile(myfile,myfilepath);
  82. {create it,if it is exists then delete its contents...}
  83. rewrite(myfile);
  84.  
  85. writeln('Unshorted array: ');
  86.  
  87. {let's write the above array to this file}
  88. reset(myfile);
  89. for i:=1 to 5 do begin
  90. write(myfile,allout[i]);
  91. writeln(i,'. ',allout[i].f,' ',allout[i].l);
  92. end;
  93. {closing...}
  94. close(myfile);
  95. {open again,now to read into an array}
  96. {$I-}
  97. reset(myfile);
  98. {$I+}
  99. if (ioresult <> 0) then begin
  100. writeln('Error: ',ioresult); {write the error code,just a number...}
  101. readln;
  102. exit;
  103. end;
  104. {read into allin}
  105. j:=1;
  106. while not eof(myfile)do begin
  107. read(myfile,allin[j]);
  108. inc(j,1);
  109. end;
  110. close(myfile);
  111.  
  112. writeln;
  113. writeln;
  114. {short them,by first name}
  115. for k:=1 to 4 do begin
  116. for p:=k+1 to 5 do begin
  117. if ( allin[k].f > allin[p].f ) then begin
  118. temp:=allin[k];
  119. allin[k]:=allin[p];
  120. allin[p]:=temp;
  121. end;
  122. end;
  123. end;
  124. {write out the results...}
  125. writeln('Shorted array: ');
  126. for x:=1 to 5 do begin
  127. writeln(x,'. ',allin[x].f,' ',allin[x].l);
  128. end;
  129.  
  130. readln;
  131. end.
  132. {by FlamingClaw 2009.09.}
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Delphi re-indexes arrays passed to functions?
Next Thread in Pascal and Delphi Forum Timeline: Need Help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC