Sort a txt File

This is a discussion on Sort a txt File within the C++ Programming forums, part of the General Programming Boards category; Hello all I've look all over and I cann't find a way to sort a txt file. This is my ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Sort a txt File

    Hello all I've look all over and I cann't find a way to sort a txt file.
    This is my code......
    Code:
    #include <iostream.h>
    #include "fstream.h"
    
    
      class Counter
    {
    private:
    	char A[100];	// array of characters
    	int n;		// character counter
    
    public:
    	Counter(int counter=0);
    	void readStr();
    	void writeStr();
    	int wordct();
       void selectionSort();
    
    };
    
    // constructor used to initialize n zero
    Counter::Counter(int counter) : n(counter)
    {}
    
    // reads file story.txt and assigns to array A
    void Counter::readStr()
    {
    
    	ifstream file;	// creates fin file object
    	file.open("test.txt");	// opens file fl.txt
    	if(!file)	// prints error if file fl.txt not opened properly
    		cout << "Could not open fl.txt" << endl;
    
    
    	while(file) // retreives a list of characters from fl.txt and stores them in A[i]
    		for(int i=0;i !='\n'; i++)
    		{
    			A[n]=file.get();		//retrives character
    			n++;	// counts the character
    		}
    }
    
    // prints file to screen
    void Counter::writeStr()
    {
    	// prints list of characters just like whats inside fl.txt
       int i=0;
       while(A[i]!='~')
       {
    		cout << A[i];
          i++;
       }
    }
    void Counter::selectionSort()
    {
       char A[100];
       int counter=0;
    	if (n > 1)
       {
        int ti=0;
        	for(int i=1; i <= counter-1; i++)
          {
        		if (A[i] > A[ti])
             {
    					ti=i;
             }
    
          				int t =  A[ti];
          				A[ti]=A[counter-1];
                      A[counter-1]=t;
                      //selectionSort(A, (counter)-1);
    }
    }
    }
    
    // free function prints menu to screen
    void menu()
    {
    	cout << " ______________________________________________________\n";
    	cout << "|                                                      |\n";
    	cout << "|         1. Read String                               |\n";
    	cout << "|         2. Print String                              |\n";
    	cout << "|         3. Count Words                               |\n";
    	cout << "|         4. Exit                                      |\n";
    	cout << "|______________________________________________________|\n";
    }
    
    // main program
    void main()
    {
    	Counter object;
    	char input;
    
    	do		//do/while loop
    	{
    		menu();	// prints menu
    		cin >> input;	// user input
    		cout << endl;
    
    		// menu selection cases
    		switch (input)
    		{
    			case '1':
    				object.readStr(); // calls readStr member function
    				cout << endl;
    				break;
    			case '2':
    				object.writeStr();	// calls writeStr member function
    				cout << endl;
    				break;
    			case '3':
             object.selectionSort();
             object.writeStr();
    				break;
    			case '4':
    				cout << "Thanks for using my program" << endl;
    				cout << endl;
    				break;
    			default :
    				cout << "Invalid input" << endl;
    				cout << endl;
    				break;
    		}
    	}while(input!='4');
    
    	cout << endl;
    
    }
    This is my text file.....
    a
    a
    b
    b
    b
    c
    c
    d
    d
    d
    e
    e

    This is the output that I want....
    a
    b
    c
    d
    e

    Nothing fancy just want a simple sort....
    I have no clue I have tried everything, any help would be apperiated. I have tried all different types of sorts but I cann't get anything wo work.
    Thank You
    ACHILLES
    Last edited by Achilles; 02-17-2003 at 07:57 PM.

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Read this, then edit your post acoordingly
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    Try placing it all in a .cpp file and then attach it to your post. I can't read it correctly from there.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by ZooTrigger1191
    Try placing it all in a .cpp file and then attach it to your post. I can't read it correctly from there.
    or you could copy and paste it.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    but I don't want to.......

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    As you read the characters from the file, insert them into a set container. They will automatically be sorted and if you use the set container rather than the multiset container, all duplicates will be thrown away for you as well. Then you just iterate through the elements in the set one at a time writting each one out to the screen or another file, whatever you wish. It's easy. That whole program you had can be rewritten in around a dozen lines of code!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21