Hi guys, I am currently doing my assignment. It requires me to create a program that will be able to create an address book using File.So far, I managed to display all the contacts, search the contacts and delete the contacts. But , I m not able to sort the name of the ones in my address book in alphabetical order..

I have some idea, and I know I must use array, but HOW??
Anyone, please give your idea... Thanks in advance..

If u don mind, could u just give me an example of the usage of such function... I haven't heard of that function before..

By the way, this is the code I m working on... I wish to store the "name" into the array, but i don know what code should I use...
Please refer the colored letters..There is where I wish to put the code...

void arrangeName(void)
{
    FILE*arrange;
    int line=0,x,result;
    char name[100],sname,arr[100],NwName[100];

    arrange=fopen("AddressBook.txt","r");

    result=fscanf(arrange,"%s",name);

    for(x=0;result==1;x++)
    {
        [COLOR="Red"]HERE IT IS[/COLOR]
                gets(name)=arr[x];
        while(fscanf(arrange,"%c",&sname)!=0)
        {
            if(sname=='\n')
                line++;
                if(line==7)
                    EOF;
        }
        result=fscanf(arrange,"%s",name);
    }

    printf("%s",arr[0]);

Edited 3 Years Ago by Reverend Jim: Fixed formatting

qsort() what your looking for.

But be careful with the example from that page. Subtracting ints like that without any kind of underflow test has some risk. The safe way is easier to understand too:

#include <stdio.h>
#include <stdlib.h>

int Compare(void const* a, void const* b)
{
    int const* first = a;
    int const* second = b;

    if (*first == *second) return 0;
    else if (*first < *second) return -1;
    else return 1;
}

int main()
{
    int a[] = {0,4,6,2,8,7,1,6,8,4,3,2,8,5,9};
    size_t const sz = sizeof a / sizeof *a;
    size_t x;

    for (x = 0; x < sz; ++x) printf("%2d%s", a[x], x < sz - 1 ? "" : "\n");
    qsort(a, sz, sizeof *a, Compare);
    for (x = 0; x < sz; ++x) printf("%2d%s", a[x], x < sz - 1 ? "" : "\n");

    return 0;
}
Comments
Nice
This article has been dead for over six months. Start a new discussion instead.