Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to read names from a file and sort them in alphabetical order

How to read names from a file and sort them in alphabetical order

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raeiko
    wrote on last edited by
    #1

    Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:

    #define SIZE 30

    int main()
    {
    char name[ SIZE ]; /* Data records */

    FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/
    
    /\* fopen opens file; exits program if file cannot be opened \*/
    if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) {
        printf( "File could not be opened\\n" );
    } /\* End if \*/
    
    /\* Read records from file \*/
    else {
        printf( "%s\\n", "Name" );
        fscanf( fpPtr, "%s", name );
        
        /\* While not end of file \*/
        while( !feof( fpPtr ) ) {
                printf( "%s\\n", name );
                fscanf( fpPtr, "%s", name );
        } /\* End while \*/
        
        fclose( fpPtr ); /\* fclose closes the file \*/
        
    } /\* End else \*/
    
    
    
    
    return 0; /\* Indicates that the program terminated successfully \*/
    

    } /* End Main */

    S D T R 4 Replies Last reply
    0
    • R raeiko

      Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:

      #define SIZE 30

      int main()
      {
      char name[ SIZE ]; /* Data records */

      FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/
      
      /\* fopen opens file; exits program if file cannot be opened \*/
      if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) {
          printf( "File could not be opened\\n" );
      } /\* End if \*/
      
      /\* Read records from file \*/
      else {
          printf( "%s\\n", "Name" );
          fscanf( fpPtr, "%s", name );
          
          /\* While not end of file \*/
          while( !feof( fpPtr ) ) {
                  printf( "%s\\n", name );
                  fscanf( fpPtr, "%s", name );
          } /\* End while \*/
          
          fclose( fpPtr ); /\* fclose closes the file \*/
          
      } /\* End else \*/
      
      
      
      
      return 0; /\* Indicates that the program terminated successfully \*/
      

      } /* End Main */

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Why write your own bubble sort when the standard library has a quicksort built in. That way, you get a fast sort routine that you know works... 99.9999% of the time, it's better to use someone else's library code rather than write your own.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      D 1 Reply Last reply
      0
      • S Stuart Dootson

        Why write your own bubble sort when the standard library has a quicksort built in. That way, you get a fast sort routine that you know works... 99.9999% of the time, it's better to use someone else's library code rather than write your own.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Stuart Dootson wrote:

        Why write your own bubble sort when the standard library has a quicksort built in.

        Perhaps it's an academic exercise.

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        S 1 Reply Last reply
        0
        • D David Crow

          Stuart Dootson wrote:

          Why write your own bubble sort when the standard library has a quicksort built in.

          Perhaps it's an academic exercise.

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          That's what I figured - in which case, helping him isn't helping him :-)

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • R raeiko

            Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:

            #define SIZE 30

            int main()
            {
            char name[ SIZE ]; /* Data records */

            FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/
            
            /\* fopen opens file; exits program if file cannot be opened \*/
            if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) {
                printf( "File could not be opened\\n" );
            } /\* End if \*/
            
            /\* Read records from file \*/
            else {
                printf( "%s\\n", "Name" );
                fscanf( fpPtr, "%s", name );
                
                /\* While not end of file \*/
                while( !feof( fpPtr ) ) {
                        printf( "%s\\n", name );
                        fscanf( fpPtr, "%s", name );
                } /\* End while \*/
                
                fclose( fpPtr ); /\* fclose closes the file \*/
                
            } /\* End else \*/
            
            
            
            
            return 0; /\* Indicates that the program terminated successfully \*/
            

            } /* End Main */

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            raeiko wrote:

            ...but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them.

            You first need to get a working bubble-sort algorithm before moving on.

            int nNumbers[] = { 1, 7, 4, 10, 3, 5, 6, 9 };
            int nCount = sizeof(nNumbers) / sizeof(nNumbers[0]);
            ...
            BubbleSort(nNumbers, nCount);

            Work on this, and when you get stuck, we'll be glad to help.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            1 Reply Last reply
            0
            • R raeiko

              Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:

              #define SIZE 30

              int main()
              {
              char name[ SIZE ]; /* Data records */

              FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/
              
              /\* fopen opens file; exits program if file cannot be opened \*/
              if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) {
                  printf( "File could not be opened\\n" );
              } /\* End if \*/
              
              /\* Read records from file \*/
              else {
                  printf( "%s\\n", "Name" );
                  fscanf( fpPtr, "%s", name );
                  
                  /\* While not end of file \*/
                  while( !feof( fpPtr ) ) {
                          printf( "%s\\n", name );
                          fscanf( fpPtr, "%s", name );
                  } /\* End while \*/
                  
                  fclose( fpPtr ); /\* fclose closes the file \*/
                  
              } /\* End else \*/
              
              
              
              
              return 0; /\* Indicates that the program terminated successfully \*/
              

              } /* End Main */

              T Offline
              T Offline
              tyr2000
              wrote on last edited by
              #6

              I think tree structer is far more better than the bubble sort in actual work, if you are doing a actual work not homework. there is less moving when the tree is used.

              1 Reply Last reply
              0
              • R raeiko

                Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:

                #define SIZE 30

                int main()
                {
                char name[ SIZE ]; /* Data records */

                FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/
                
                /\* fopen opens file; exits program if file cannot be opened \*/
                if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) {
                    printf( "File could not be opened\\n" );
                } /\* End if \*/
                
                /\* Read records from file \*/
                else {
                    printf( "%s\\n", "Name" );
                    fscanf( fpPtr, "%s", name );
                    
                    /\* While not end of file \*/
                    while( !feof( fpPtr ) ) {
                            printf( "%s\\n", name );
                            fscanf( fpPtr, "%s", name );
                    } /\* End while \*/
                    
                    fclose( fpPtr ); /\* fclose closes the file \*/
                    
                } /\* End else \*/
                
                
                
                
                return 0; /\* Indicates that the program terminated successfully \*/
                

                } /* End Main */

                R Offline
                R Offline
                raeiko
                wrote on last edited by
                #7

                Hello, I'm writing this program for an academical assignment that is why i need to use the bubble sort algorithm, even though, from what i undersand, is not the most suitable solution. I have tried to go on with my code but it's still not working and i'm getting more and more confused because i don't have really clear in my mind how to handle pointers... Here is what i have done but i'm sure it's completely wrong

                /* Program that takes a text file as input and produces an output file that has all the original lines in alphabetical order */

                #include <stdio.h>
                #include <string.h>
                #define SIZE 30

                int main()
                {
                char name[ SIZE ]; /* Data records */

                FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/
                
                /\* fopen opens file; exits program if file cannot be opened \*/
                if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) {
                    printf( "File could not be opened\\n" );
                } /\* End if \*/
                
                /\* Read records from file \*/
                else {
                    printf( "%s\\n", "Name" );
                    fscanf( fpPtr, "%s", name );
                    
                    /\* While not end of file \*/
                    while( !feof( fpPtr ) ) {
                            printf( "%s\\n", name );
                            fscanf( fpPtr, "%s", name );
                    } /\* End while \*/
                    
                    fclose( fpPtr ); /\* fclose closes the file \*/
                    
                } /\* End else \*/
                

                /* sort */
                for ( pass = 1; pass < SIZE; pass++ ){ /* loop to cotrol passes */
                for ( counter = 0; counter < SIZE - 1; counter ++ ){ /* loop to control number of comparesons per pass */
                if ( *name [ counter ] > *name [ counter + 1 ] ) {
                hold = *name[ counter ];
                *name[ counter ] = *name[ counter + 1 ];
                *name[ counter + 1 ] = hold;
                } /* End if */
                }/* end of inner for */
                }/* end of outer for */

                return 0; /\* Indicates that the program terminated successfully \*/
                

                } /* End Main */

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups