Solved

Sorting Text

Posted on 1998-03-15
8
238 Views
Last Modified: 2010-04-10
Has anyone writting a program that asks the user for a text file and then the program sorts the
lines alphabetically and saves the result to another file?  For example, a sample run of the
program might be:

     Enter name of text file to be sorted:  C:\filename.txt
     Enter name of output file:  C:\filename2.txt
     Sorted C:\filename.txt and saved to C:\filename2.txt
0
Comment
Question by:strider031598
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:vsinha
ID: 1183509
Yes

I have implemented it by reding the text a line at a time and inserting them into a linked list.

However most Operating Systems have a very powerful sort utility which may be easier to use than writing a program.

Do you want a more detailied algorithm or source-code?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183510
Is this a homework assignment?
0
 

Author Comment

by:strider031598
ID: 1183511
vshina:  yes, I would like a source code.
nietod:  no, it's not a homework assignment.  I need a quick program that will
            sort text alphabetically (I'm writing my collection of CDs on a .txt file).
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:footloose
ID: 1183512
Simple way: go to DOS prompt, say:

type unsorted.txt | sort > sorted.txt

If you insist on a program: RTFM on qsort.
0
 

Author Comment

by:strider031598
ID: 1183513
What's RTFM on qsort?
0
 
LVL 1

Expert Comment

by:Nexial
ID: 1183514
Simple solution is mentioned above:

  sort < usorted.txt > sorted.txt

where sort is the standard DOS sort filter.

Try 'help sort'  or sort /?  on your system at the DOS prompt.

The mks toolkit has a better sort filter if necessary.

The unix sort is at least as good as the mks one.

RTFM = READ THE (bleeped out) MANUAL

To write a program in C or any of it's derivatives to do this
use C's qsort function call, well documented in any standard
manual or on the MAN pages of any UNIX style system.   It is also in the standard on-line help of the DOS/WINxxx run time packages.

If you want to implement the quicksort (qsort) algorithm yourself, I highly recommend Knuth's "Sorting and Searching", Volume III of "The Art of Computer Programming", which covers the theory in detail.

0
 

Author Comment

by:strider031598
ID: 1183515
Does anyone have the source to this program?
0
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 200 total points
ID: 1183516
RTFM means Read The ****ing Manual...it's a UNIX thing
A program layout using STL:
  Read the file line by line, each line into a string
  Store the strings in a vector<string>
  sort the vector
  write the vector to a new file

// Begin of code file
#include <string>
#include <vector>
#include <iostream.h>
#include <fstream.h>
#include <algorithm>

int main()
{
ifstream in("infile.txt");
vector<string> sorted;
while(!in.eof())
{
  string tmp;
  in >> tmp;
  sorted.insert(tmp);
}

ofstream out("sorted.txt");
sort(sorted.begin(), sorted.end()); // or: sort(sorted.begin(), sorted.end(), less<string>);
for(vector<string>::iterator i(sorted.begin()); i != sorted.end(); ++i)
  out << *i;

return 0;
}
// End of File

Or something like it. Its not tested or even compiled but it should give you an idea of a simple approach to the problem.

.luc.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question