944,032 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6029
  • C++ RSS
Feb 17th, 2006
0

sort string in file txt

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. small
  2. tall
  3. big
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 18th, 2006
0

Re: sort string in file txt

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++.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

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: C++ program error
Next Thread in C++ Forum Timeline: broken code





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


Follow us on Twitter


© 2011 DaniWeb® LLC