Thread: alphabetizing an input file?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    alphabetizing an input file?

    Hi i'm new to programming and would like to alphabetize my input file

    i have a list of names

    henry
    james
    etc
    ...

    but i cannot find any reputable information and my tutors are very bad

    Can you please help?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The easy way (relatively speaking) is to read the entire file into an array (or linked list) then sort it. If you have an array, the standard qsort() function can be used. For a linked list, you can use insertion sort, although that's slow. A more complex but efficient method to sort a linked list is a merge sort.

    You'll have to start by writing some code, though.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    first... look up the syntax for strcmp() ... next read up a little on various sorts such as Bubble Sort, Selection Sort, Quick Sort etc...

    Basically it's like those sliding tile games where you can only move one tile at a time... you will be scanning your string array, multiple times, deciding when to trade items to move things into order.

    There's a ton of information out there, Google is your friend...

    Once you get to the point where you have some code, if you get stuck... post your code here and we'll see what we can do.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    #include "stdafx.h"
    #include <stdio.h>
    Code:
    int
        main(void)
    {
        FILE *fp ;
        char ch ;
        fp = fopen ( "input2a.dat", "r" ) ;
        while ( 1 )
        {
        ch = fgetc ( fp ) ;
        if ( ch == EOF )
        break ;
        printf ( "%c", ch ) ;
        }
        fclose ( fp ) ;
        return 0;
    }
    This reads the data file in question. I am having difficulty finding a decent site for help on sorting functions to alphebetize it

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well first of all you need to read by lines or words... not characters, unless you plan to do a lot of post-processing.

    Try to work out how you can read your data file into an array of char strings...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you compiling this program in C or C++? Check your project options and see that you are using the compiler you need.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    I am writing in c,

    I have come to the conclusion that i should quit life and form a suicide pact with everyone else who is going to fail this assignment.

    P.s With the code i posted, i have determined that i should be using an array. Taken me 3 hours. No progress.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alphabetizing Names
    By thestrapman in forum C Programming
    Replies: 6
    Last Post: 08-21-2008, 12:36 AM
  2. Alphabetizing a file?
    By TitoMB345 in forum C Programming
    Replies: 14
    Last Post: 11-07-2007, 03:52 PM
  3. alphabetizing?
    By Cudarich in forum C Programming
    Replies: 1
    Last Post: 12-09-2004, 03:20 AM
  4. alphabetizing
    By mouse163 in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2002, 03:59 PM
  5. Need help alphabetizing a Struct
    By Sapphire19 in forum C Programming
    Replies: 4
    Last Post: 11-28-2001, 12:47 PM