void main ( ) { clrscr(); void create ( ); void ftraverse ( ); void btraverse ( ); create ( ); btraverse ( ); ftraverse ( ); getch ( ); }
/* load and initialize db from file first menu option function */ void init_db(const char *file) { /* temp buffers and descriptors */ FILE *fd; char given_id[11]; char line[256+56+56+4+40]; char book_name[256]; char writer[56*2]; char the_year[4]; char pub_name[40]; fd = fopen(file, "r"); if (!fd) fatal("Is there a datafile?"); /* get the total number of books */ if (!fgets(line, 8, fd)) fatal("while reading datafile"); long n = atol(line); if (!n) fatal("no sum of books at the first line"); /* allocate the main database */ db.arr = smalloc(sizeof(Book)*n); db.numberOfBooks = 0; /* start parsing the file, delimiter is ';' */ memset(line, 0, sizeof(line)); printf("Loading file...\n"); while (fgets(line, sizeof(Book), fd) != NULL) { /* erase the previous data */ memset(given_id, 0, sizeof(given_id)); memset(book_name, 0, sizeof(book_name)); memset(writer, 0, sizeof(writer)); memset(the_year, 0, sizeof(the_year)); memset(pub_name, 0, sizeof(pub_name)); /* scanf is like magic */ sscanf(line, "\"%[0-9]\";\"%[0-9a-zA-Z.:!'?,)( ]\";\"%[0-9a-zA-Z.:',!?)( ]\";\"%[0-9]\";\"%[0-9a-zA-Z.:'!,?)( ]\"", given_id, book_name, writer, the_year, pub_name); /* the symbols in the scanf sequence are the only ones we accept to * be in the strings and we don't want any weird stuff because they * break the encoding, so we're dropping any books that apper to have * no authors */ if (strlen(writer) == 0) { memset(line, 0, sizeof(line)); continue; } /* set defaults here */ if (strlen(the_year) == 0) strcpy(the_year, "0000"); /* check for duplicates */ if (btraverse(atol(given_id), 1) != -1) continue; /* Done parsing. Creating book */ Book *B; B = create_book(atol(given_id), book_name, atoi(the_year), pub_name); /* creating authors */ split_create_authors(B, writer); /* put it in the actual database */ db.arr[db.numberOfBooks] = *B; db.numberOfBooks++; /* clean */ memset(line, 0, sizeof(line)); /* we copy the data above, so we don't need that */ free(B); } fclose(fd); }