| | |
sort name in file.txt
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 13
Reputation:
Solved Threads: 0
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........
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........
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) ...

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."
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
I have an idea:-
1- make a structure with the following member.
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??!!
1- make a structure with the following member.
CPP Syntax (Toggle Plain Text)
struct Record{ char name [20]; };
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.
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...
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.
![]() |
Similar Threads
- import data from file .txt to table (Python)
- how take the firewall log file which is .txt file format into the database using Vb (Visual Basic 4 / 5 / 6)
- Problem with Sort() file TXT (C++)
- dat file txt file help???? (C)
- Bubble Sort of a file of records in Pascal pls (Pascal and Delphi)
- sort string in file txt (C++)
- Bubble sort & File output jibrish errors??? (C)
Other Threads in the C++ Forum
- Previous Thread: Date Function not returning variable in main
- Next Thread: Help required to add a number with a certian probability!!!
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






