How to read and sort data in a textfile (ascending order)

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

Join Date: Oct 2009
Posts: 5
Reputation: chubzyko is an unknown quantity at this point 
Solved Threads: 0
chubzyko chubzyko is offline Offline
Newbie Poster

How to read and sort data in a textfile (ascending order)

 
0
  #1
31 Days Ago
I've been surfing the net for hours now looking for a good example of reading and sorting data from a textfile. But I can't find a good one that is working. Can somebody give me a simple example code of reading and sorting data from a textfile in ascending order plz? The textfile will just contain numbers and name just like this one

5 jose
2 juan
3 miguel
1 luis
4 manuel
and when read and sorted, it will be displayed like this

1 luis
2 juan
3 miguel
4 manuel
5 jose
Thanks in advance for someone who could help me.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 2
power_computer power_computer is offline Offline
Junior Poster
 
0
  #2
31 Days Ago
Do you have any code you worked on at all?
As the rules state above this section, we help those who show work.

I'll help you get started

  1. #include <iostream>
  2. #include <fstrea> file input output stream library
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ifstream fin; //declare fin as a variable of input file stream which is used to read IN from a file
  8.  
  9. fin.open("text.txt"); //open file
  10.  
  11. //Read from file
  12. //Store data in array, or array of structs.
  13. //Use a sorting algorithm to put the data in order.
  14.  
  15.  
  16. return 0;
  17. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: chubzyko is an unknown quantity at this point 
Solved Threads: 0
chubzyko chubzyko is offline Offline
Newbie Poster
 
0
  #3
31 Days Ago
tnx for that but I already have a read code. What I don't know how to make is the looping and stroring of data in an array. Can you give me a simple example code of that plz? And i'll just applly it to my program.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #4
31 Days Ago
Do you know how many items or a maximum number of items there are in the file? If not an array probably isn't the best container to hold the data, unless that it is the container type you are required to use.

Do you know about classes and structs, because they will make the task much easier.

The basic protocol for loading the array might look something like this:
  1. declare an int to act as an index and initialize it to an appropriate value
  2. declare an array of class objects of an appropriate size
  3. declare an ifstream and associate it with an appropriate file
  4.  
  5. if unable to open file
  6. {
  7. tell user problem occured and exit program or do something more elegant
  8. }
  9.  
  10. while (you are able to read in an object into the current element of the array using the ifstream and the number of objects is below the capacity of the array)
  11. {
  12. increment the current index
  13. }
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC