Question:
How to make C++ program that arranges info in a .txt file alphabetically?
kumar
2009-05-18 06:08:52 UTC
how do you make a program in C++ that takes in a song list .txt file then arranges the songs in the .txt file alphabetically?
Three answers:
cja
2009-05-18 12:10:49 UTC
A previous responder had the right idea, but in C++ it's easiest to use a vector, and the sort function from . Like this:



#include

#include

#include

#include



using namespace std;



void print(const vector&);



int main(int argc, char *argv[]) {

    string line;

    vector sVec;

    ifstream infile;



    if (argc > 1) {

        ifstream infile(argv[1]);

        if (infile.is_open()) {

            while (infile) {

                getline(infile,line);

                if (line.size() > 0) {

                    sVec.push_back(line);

                }

            }

            infile.close();

            print(sVec);

            sort(sVec.begin(),sVec.end());

            cout << endl << "Sorted: " << endl;

            print(sVec);



            // if overwrite of input file is needed,

            // reopen argv[1] for writing, iterate

            // through sVec, and insert each line

            // into the ofstream.



        } else {

            cout << argv[1] << " not found." << endl;

            return -1;

        }

    } else {

        cout << "usage: " << argv[0] << " " << endl;

        return -1;

    }

    return 0;

}



void print(const vector &v) {

    vector::const_iterator i;

    for (i = v.begin(); i != v.end(); i++) {

        cout << *i << endl;

    }

}





#if 0



Sample run:



$ ./sortTst sortTst.dat

Revealing Science of God -- Dance of the Dawn

Remembering High the Memory

Ancient Giants Under the Sun

Ritual - Nous Sommes du Soleil



Sorted:

Ancient Giants Under the Sun

Remembering High the Memory

Revealing Science of God -- Dance of the Dawn

Ritual - Nous Sommes du Soleil



#endif
no1home2day
2009-05-18 06:19:20 UTC
What you need is a sorting algorithm.



There are quite a few, and it would be difficult to suggest only one.



Dijkstra (spelling?) (that's a person's name) specializes in sorting algorithms, and teaches how to implement the various algorithms, but to say that one is better than another would be doing you an injustice, because each one has it's strong points and it's weak points.



The EASIEST (and SLOWST) is called the "bubble sort", in which you compare each element to every other element, "bubbling" the 1st element to the top of the array, then the 2nd element, the 3rd, etc.



It would look something like this:



FOR i=1 to LastElement-1

... FOR j=i+1 to LastElement

... ... If Array(i) > Array(j) then

... ... ... Temp = Array(i)

... ... ... Array(i) = Array(j)

... ... ... Array(j) = Temp

... ... End If

... Next j

Next i



The stuff in the IF-THEN statement switches the two elements, so each element bubbles up to the top of the list.



Like I said, this is the easiest to understand, but it's also the very slowest.
Paul
2009-05-18 06:17:06 UTC
- open the file for reading

- read the entire contents of the file into an array

- close the file

- sort the array (e.g use qsort)

- open the file for writing

- write the array to the file

- close the file


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...