Reading External Text Files

How do I read in a list of names from an external text file? I would like to read in two lists of alphabetized names from separate text files and merge them into one alphabetical list.  I would also like to print out any name from either of the two external lists, say the 5th name of list2.  Any help in this matter would be greatly appreciated.  
willie117Asked:
Who is Participating?
 
BigRatConnect With a Mentor Commented:
Yes, you would have an array of PChar :-
const
   MaxCount = 1000;
type
   TNameArray = array[1..MaxCount] of PChar;
var
   NameArray : TNameArray;
   Name        : packed array[0..60 of Char];
   Leng         : Integer;

   Leng:=0;
   while not Eof(Input) do
      begin
      Readln(Input,Name);
      Inc(Leng);
      NameArray[Leng]:=StrNew(Name);
      end;

The elements are then 1..Leng.

For further Info (if you need it) reject this answer.
0
 
willie117Author Commented:
e-mail is jayleff@aol.com
0
 
FuzzyLogicCommented:
To merge two ordered lists into one list is easy, and I can give you basic code for that.

However, when you said "print out any name...", do you mean that:
A. The program waits for user query and writes out the answer  (and wait for another query)?
B. The user give his query at command-line and the program processes his query?

Fuzzy
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
willie117Author Commented:
Once the list is compiled, the user then can query, say for the 4th name in the list.
0
 
BigRatCommented:
It really depends how big the files are and how long the names are. If the files are relatively small and the names less than say 60 characters you can hold all of it in memory. For example assume the names are on one line in the file :-
       Homer Simpson
       Heffer Wolf
       Barney Rubble
    and so on, up to say several hundred, the creating a list like :-

     PNameList = ^TNameList;
     TNameList = record
                 Next : PNameList;
                 Name : PChar;
                 end;
   while not(EOF(Input)) do
      begin
      Readln(Input,Name);
      new(Entry);
      Entry^.Next:=List;
      List^.Name:=StrNew(Name);
      List:=Entry;
      end;
in memory gives you a simple list. All the names are stored on the heap. If this is waht you are thing about reply and we'll lok further!. If not ignore it!
     have ratitude
0
 
willie117Author Commented:
That looks good, the files are small and the names are less than 60.  Also is it possible to use an array with this?
0
 
willie117Author Commented:
Thank you for your answer.  It is most helpful.  One last question though (I'm not the best programmer) how do I access the individual elements?  Thanks.  Have ratitude!
0
 
willie117Author Commented:
I think I asked my question too fast: I think I access the elements with: NameArray[1], NameArray[2], ... or with NameArray[Leng].  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.