sort string in file txt

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

Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

sort string in file txt

 
0
  #1
Feb 17th, 2006
can anybody give me an idea how to sort string in alphabetical order in file.text after user input string and in the file already had a list of string?
which is the best,let user input the all string first then sorting it or sorting if after user input the string?

  1. #include <iostream.h>
  2. #include <fstream.h>
  3.  
  4. void main()
  5. {
  6.  
  7. fstream kamus("Adkjektif.txt",ios::in | ios::out | ios::app);
  8. const size = 30;
  9. char kata2[size];
  10. cout<<"input string (end for stop)"<<endl;
  11. for(;;)
  12. {
  13.  
  14. cin.getline(kata2,size);
  15. if (strcmpi(kata2,"end")==0)
  16. break;
  17. else
  18. kamus<<kata2<<endl;
  19.  
  20. }
  21. kamus.close();
  22. }

this is the content of the file
  1. small
  2. tall
  3. big
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,577
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: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: sort string in file txt

 
0
  #2
Feb 18th, 2006
there are methods of sorting text files, but they are generally pretty complicated. If the file is small enough, just read all the strings into an in-memory array, sort the array, then rewrite them back to the file. If you are writing c++ program that is pretty easy -- read the strings into a std::vector<std::string> array, then use std::sort() to sort it.

Use a pretty recent compiler such as Dev-C++. The code you posted uses iostream.h which has been obsolete for several years, so I suspect you are using a pretty ancient compiler such as Turbo C++.
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