Zombie
Zombie

Reputation: 341

Sorting name of books by alphabetical order

I'm developing an application and one of the functions is the function sort_books_file which basically has to sort the name of the books in the file fp by alphabetical order and then prints them in the file fp2.

At the moment, the only thing the function does is printing the name of the books from file fp into file fp2.

I would like to know how it is possible to sort the name of the books by alphabetical order into the file fp2.

I'm a C beginner and I don't have a lot of experience in C programming...somebody helps?

#include <stdio.h>    
FILE *fp;
FILE *fp2;

struct book{
   int key;
   char name[50];
   int price;
 };

sort_books_file(){

    struct book b;

    //r: open the file for reading (read-only)
    if ((fp=fopen("books.dat","r"))==NULL){
        printf("Error\n");
        exit(1);
    }

    //w: open the file for writing (write-only).
    //the file is created if it doesn't exist
    if ((fp2=fopen("books_sorted.dat","w"))==NULL){
        printf("Error: not possible to open the file. \n");
        exit(1);
    }

    //while end of file has not been reached
    while (!feof(fp)){  

        fread(&b,sizeof(b),1,fp);

        if(feof(fp))
        {
        break;
        }

        fwrite(&b.name,sizeof(b.name),1,fp2);

    }

    fclose(fp);
    fclose(fp2);
}

Upvotes: 0

Views: 1853

Answers (2)

Kevin Martin Jose
Kevin Martin Jose

Reputation: 640

The easiest way is to use strcmp() on book.name to sort.

strcmp() works as follows :

syntax : int strcmp(const char *str1, const char *str2)

st1 and str2 are the strings to be compared

The function returns -1 if str1 is less than str2 and 0 if the strings are equal and 1 if str1 is greater than str2.

strcmp() uses lexicographic ordering, which means it sorts the words as it appears on the dictionary. Here's a question that discusses it.

Examples

strcmp("hello", "world")returns -1

strcmp("world", "hello") returns 1

stcmp("boo", "boo") returns 0

And here's a sorting function that does what you want (I haven't tested it) :

void sort_books_file(){

 //Assume you have only maximum 10 books
 struct book books[10]; 
 strct book b;

 //open files for reading and writing
 //..
 //..

int i = 0;
while (!feof(fp)){  

        fread(&b,sizeof(b),1,fp);
        books[i] = b;
        i++;
        if(feof(fp))
        {
        break;
        }
    }

//number of books
int len = i;
//bubble sort;
int j = 0;

//Bubble sort. Google for "bubble sort"
for(i=0; i<len; i++)
{
    for(j=0; j<len-1; j++)
    {
       //If the first book should come after the next book in the array
       if(strcmp(books[j].name, books[j+1].name) > 0)
        {
            //swap the books
            struct book temp;
            temp = books[j];
            books[j] = books[j+1];
            books[j+1] = temp;
        }
    }
}

//now write each book in the array "books" into the file one by one
}

Upvotes: 2

Shahriar
Shahriar

Reputation: 13804

I hope this will help:

void sort(struct book* books, int n)
{
    int j,i;

   for(i=1;i<n;i++)
   {
       for(j=0;j<n-i;j++)
       {
           if(books[j].name < books[j+1].name)
           {
               struct book temp = books[j];
               books[j] = books[j+1];
               books[j+1] = temp;
          }
      }
  }
}

Store Book info into an array of book structure. Then pass this array to sort function. This will do your job.

struct book LIST[n];
sort(LIST, n);

Upvotes: 2

Related Questions