struct Record* load_Record(FILE* infile) { struct Record* new_record; int ID, rating; char medium[ MEDIUM_MAX_SIZE ]; char title [ TITLE_MAX_BUFF_SIZE ]; /* read in from the file making sure not to overflow the buffer */ if ( fscanf( infile, "%d %" STRINGIFY( MEDIUM_MAX_SIZE )"s %d\n", &ID, medium, &rating ) != 3 ) return NULL; if ( get_title( infile, title ) ) return NULL; new_record = create_Record( medium, title ); /* update it's member vars from the defaults to the input values */ new_record->rating = rating; new_record->ID = ID; /* set Record ID to the max value we have seen loading from the file and decrement it back to what it was before create_Record() was called */ if( ID > Record_ID_counter ) Record_ID_counter = ID + 1; else Record_ID_counter--; return new_record; }
static void add_record( struct Ordered_container* lib_title, struct Ordered_container* lib_ID ) { char title [ TITLE_ARRAY_SIZE ]; char medium[ MEDIUM_ARRAY_SIZE ]; struct Record* new_rec; /* read the record in from the command line */ if ( !get_medium_and_title( medium, title ) ) { print_error( "Could not read a title!\n" ); return; } /* check if the Record already exists */ if ( OC_find_item_arg( lib_title, title , comp_Record_to_title ) != NULL ) { print_error( "Library already has a record with this title!\n" ); return; } else { new_rec = create_Record(medium, title ); OC_insert( lib_title, new_rec ); OC_insert( lib_ID, new_rec ); printf( "Record %d added\n", get_Record_ID( new_rec ) ); } }
struct Record* load_Record(FILE* infile) { int record_id, rating; char medium[RECORD_MEDIUM_SIZE]; char title[RECORD_TITLE_SIZE]; struct Record *new_record; char fmt_str[30]; sprintf(fmt_str, "%%d %%%ds %%d", RECORD_MEDIUM_SIZE-1); if (fscanf(infile, fmt_str, &record_id, medium, &rating) != 3) return NULL; fgetc(infile); /* read the leading space */ if (!fgets(title, RECORD_TITLE_SIZE, infile)) { return NULL; } title[strlen(title)-1] = '\0'; /* remove the newline character */ new_record = create_Record(medium, title); set_Record_rating(new_record, rating); new_record->ID = record_id; id_number_counter--; /* set id_number counter to the largest record_id read in */ if (record_id > id_number_counter) id_number_counter = record_id; return new_record; }
int main() { struct Collection * c1; struct Collection * c2; struct Collection * c3; struct Collection * c4; struct Record * r1; struct Record * r2; FILE * file; c1 = create_Collection("ASDF"); c2 = create_Collection("junk"); if (strcmp(get_Collection_name(c1), "ASDF")) { printf("ERROR: incorrect name\n"); } print_Collection(c1); if (!Collection_empty(c1)) { printf("ERROR: Collection should be empty\n"); } r1 = create_Record("MED", "TITLE"); if (add_Collection_member(c1, r1)) { printf("ERROR: Failed to add record!\n"); } if (!add_Collection_member(c1, r1)) { printf("ERROR: Should not be able to add item again\n"); } print_Collection(c1); if (Collection_empty(c1)) { printf("ERROR: Collection shouldn't be empty\n"); } r2 = create_Record("ABC", "123"); add_Collection_member(c1, r2); if (!is_Collection_member_present(c1, r2)) { printf("ERROR: Record not found\n"); } print_Collection(c1); if (remove_Collection_member(c1, r2)) { printf("ERROR: Could not remove record\n"); } print_Collection(c1); if (is_Collection_member_present(c1, r2)) { printf("ERROR: record should not be present\n"); } if (!remove_Collection_member(c1, r2)) { printf("ERROR: Record shouldn't be in the collection\n"); } add_Collection_member(c1, r2); print_Collection(c1); file = fopen("tmp", "w"); save_Collection(c1, file); save_Collection(c2, file); fclose(file); destroy_Collection(c1); destroy_Collection(c2); file = fopen("tmp", "r"); /*c3 = load_Collection(file, XXX); c4 = load_Collection(file, XXX); print_Collection(c3); print_Collection(c4); destroy_Collection(c3); destroy_Collection(c4);*/ return 0; }