943,515 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1244
  • C++ RSS
May 30th, 2009
0

sort name in file.txt

Expand Post »
Hi for ever

can any one help me only give me idea of this question


Write a program that reads from a file list of unsorted names and sort the names automatically and then asks the user whether he/she would like to print the names in the output screen or write the sorted names in a file where its name is given.
Hint:
- The reading file could be any text file where its name is given by the user




thanks a lot........
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ameerah is offline Offline
13 posts
since Nov 2008
May 30th, 2009
0

Re: sort name in file.txt

Read all the names into a vector and use the sort algorithm to sort them alphabetically
You could also sort it by using the strcmp function if vectors aren't allowed.

If both of them aren't allowed then you'll have to implement a sorting function first
(you could take a look at this code snippet in that case, it does exactly the same as the strcmp function) ...
Last edited by tux4life; May 30th, 2009 at 6:28 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 30th, 2009
0

Re: sort name in file.txt

If you have to write your own sort function, google for "sort algorithms", or for "bubble sort"
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,945 posts
since Aug 2005
May 30th, 2009
-1

Re: sort name in file.txt

I have an idea:-
1- make a structure with the following member.
CPP Syntax (Toggle Plain Text)
  1. struct Record{
  2. char name [20];
  3. };

this structure is for using fixed length record

2- Enter the names and make sure when u enter it pad the remaining bytes with spaces.
for example:- when entering mike (its 4 bytes, put 15 spaces and the last character is NULL.
I made this to make seeking in the file easier .seek 2*20 if u need the second record. 15*20 if u need 15th record etc..
4-Read names by reading 20 bytes ... 20 bytes until EOF .

5-then use a bubble sort algorithm or any other sorting algorithm
to sort the records according to their ascii values and then print them in the user's screen.

Capiche??!!
Last edited by Ahmed_I; May 30th, 2009 at 6:54 pm.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
May 31st, 2009
0

Re: sort name in file.txt

Just declare an array of strings. Read the strings from the file using fscanf and store it in the array and sort them using bubble sort.
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
May 31st, 2009
0

Re: sort name in file.txt

Probably the best method to build name list dictionary is:
1. Define std::map<std::string,unsigned> (dictionary name-counter)
2. Read names and insert them into the map (increment name counter if the name is in the map)
3. Traverse map with iterator: you will get an ordered (automatically)name list with counters.

If the file contains names separated by spaces then step #2 is trivial one. If not (it's an arbitrary text) - you need more complex scanner algorithm to extract names from a text stream...
Last edited by ArkM; May 31st, 2009 at 1:32 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

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 C++ Forum Timeline: Date Function not returning variable in main
Next Thread in C++ Forum Timeline: Help required to add a number with a certian probability!!!





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


Follow us on Twitter


© 2011 DaniWeb® LLC