Need help.
I read all about qsort, and tried qsort and lots of my ideas but I just can't get alphabet order.
Maybe I need to make some changes in program, or linked struct or something. open for all ideas. This program works me at home.
Can you help me in finishing function -- void list_records(FILE*);
-- case '2'.
Sorting struct from file in alphabet order. Does not need to be in the same file. No problem for creating another file or something.
in advance:
Thank's for your time and help.
Code:
#include <stdio.h>
#include <string.h>
struct book {
char name[30];
char publisher[30];
int year;
}data;
void add_records(FILE*);
void list_records(FILE*);
void modify_records(FILE*);
int main()
{
FILE *t, *p; int choice; int choice_search; char booknames[30];
if((t = fopen("BOOK.DAT","rb+"))==NULL)
{
if((t = fopen("BOOK.DAT","wb+"))==NULL)
{
printf("The File can't be open\n");
exit(1);
}
}
system("cls");
printf(" BOOK DIRECTORY \n");
printf(" ****************************** \n");
printf("1 - Add Records\n"
"2 - List Records\n"
"3 - Modify Records\n"
"4 - Delite Records\n"
"0 - Exit\n"
"Your Chice:\n");
fflush(stdin);
choice = getche();
switch(choice)
{
case '1': add_records(t); break;
case '2': list_records(t); break;
case '3': modify_records(t); break;
case '4':
printf("\nEnter name of book to delete:\n");
scanf("%s", booknames);
p = fopen("TEMP.DAT","wb");
rewind(t);
while(fread( &data, sizeof(data), 1, t) ==1 )
{
if(strcmp(data.name,booknames)!=0)
fwrite( &data, recsize, 1, p);
}
fclose(t); fclose(p);
remove("BOOK.DAT"); rename("TEMP.DAT","BOOK.DAT");
main();
break;
case '0': printf("\n"); fclose(t); exit(1);
}
fclose(t);
printf("\n\nError! Unknown Choice!\n\n");
main();
return 0;
}
void add_records(FILE*t)
{
char another;
fseek(t, 0, SEEK_END);
another = 'y';
while(another == 'y')
{
printf("\nEnter Book name, Publisher and Year of publishing\n");
gets(data.name); gets(data.publisher); scanf("%d",&data.year);
fwrite(&data, sizeof(data),1,t);
printf("Add another data (y/n):");
fflush(stdin);
another = getche();
printf("\n");
} fclose(t);
printf("\n");
main();
}
void list_records(FILE*t)
{
int listc; int i; int n = 0;
rewind(t);
printf("\nListanje po:\n"
"1 - Time Record\n"
"2 - Alphabet order\n");
fflush(stdin);
listc = getche();
switch(listc)
{
case '1':
printf("\n");
printf("---------------------------------------------------------------------\n");
printf("| BOOK | PUBLISHER | YEAR |\n");
printf("---------------------------------------------------------------------\n");
rewind(t);
while(fread( &data, sizeof(data), 1, t) == 1)
printf("|%-30s|%-30s| %6d|\n", &data.name,&data.publisher,data.year);
printf("------------------------------------------------------------------\n");
system("PAUSE");
printf("\n"); break;
case '2':
printf("\nNot finished at this moment\n"); break;
} fclose(t); printf("\n");
main();
}
void modify_records(FILE *t)
{
char another = 'y'; char bookname[30];
while(another == 'y')
{
printf("\nEnter book name to modify:");
gets(bookname);
rewind(t);
while(fread( &data, sizeof(data), 1, t ) == 1)
{
if(strcmp(data.name, bookname) == 0)
{
printf("\nEnter new name, publisher and year of publishing\n");
gets(data.name); gets(data.publisher); scanf("%d", &data.year);
fseek(t, -sizeof(data), SEEK_CUR);
fwrite(&data, recsize, 1, t);
break;
}
}
printf("Modify another record (y/n):");
fflush(stdin);
another = getche();
printf("\n");
} fclose(t); printf("\n");
main();
}