sort name in file.txt

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 13
Reputation: Ameerah is an unknown quantity at this point 
Solved Threads: 0
Ameerah Ameerah is offline Offline
Newbie Poster

sort name in file.txt

 
0
  #1
May 30th, 2009
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........
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: sort name in file.txt

 
0
  #2
May 30th, 2009
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: sort name in file.txt

 
0
  #3
May 30th, 2009
If you have to write your own sort function, google for "sort algorithms", or for "bubble sort"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: sort name in file.txt

 
-1
  #4
May 30th, 2009
I have an idea:-
1- make a structure with the following member.
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: sort name in file.txt

 
0
  #5
May 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: sort name in file.txt

 
0
  #6
May 31st, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC