954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sort string in file txt

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?

#include <iostream.h>
#include <fstream.h>

void main()
{

	fstream kamus("Adkjektif.txt",ios::in | ios::out | ios::app);
   const size = 30;
   char kata2[size];
   cout<<"input string (end for stop)"<<endl;
   for(;;)
   {

   	cin.getline(kata2,size);
   	if (strcmpi(kata2,"end")==0)
   		break;
         else
   	 kamus<<kata2<<endl;

   }
	kamus.close();
}

this is the content of the file

small
tall
big
amen
Newbie Poster
17 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

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 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++.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You