int main( int argc, char* argv[] ){ int i,x; int fdhp1; srand( time(NULL) ); BF_Init(); //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// if ( (fdhp1 = HP_OpenFile("hp1")) < 0 ) { BF_PrintError("Error opening file hp1"); return -1; } x=15; printf("\nSearching for records with id 15 in file hp1\n"); HP_GetAllEntries(fdhp1, "id", &x); printf("\nSearching for records with name VASSILIS in file hp1\n"); HP_GetAllEntries(fdhp1, "name", "VASSILIS"); printf("\nDeleting records with name VASSILIS in file hp1\n"); if ( HP_DeleteEntry (fdhp1, "name", "VASSILIS") < 0) { BF_PrintError("Error deleting record in file hp1"); HP_CloseFile(fdhp1); return -1; } printf("\nSearching for records with name VASSILIS in file hp1\n"); HP_GetAllEntries(fdhp1, "name", "VASSILIS"); /* No entries found */ i = 0; while ( i < 15 ) /* Insert 15 records in heap file */ { Record r = randomRecord(); if( HP_InsertEntry (fdhp1, r) < 0) { BF_PrintError("Error inserting record in file hp1"); HP_CloseFile(fdhp1); return -1; } printf("\nRecord (%d, %s, %s, %s) was successfully inserted in file hp1\n", r.id, r.name, r.surname, r.city); i++; } /* Print everything that exists in file hp1 */ printf("Printing everything in file hp1\n"); HP_GetAllEntries(fdhp1, NULL, NULL); x=12; printf("\nDeleting records with id 12 in file hp1\n"); if ( HP_DeleteEntry (fdhp1, "id", &x) < 0) { BF_PrintError("Error deleting record in file hp1"); HP_CloseFile(fdhp1); return -1; } printf("\nSearching for records with surname PAPAPETROU in file hp1\n"); HP_GetAllEntries(fdhp1, "surname", "PAPAPETROU"); if( HP_CloseFile(fdhp1) < 0) { BF_PrintError("Error closing file hp1"); return -1; } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// return 0; }
int main() { int athletesFd; int athletesBlockNum; char *athletesBlockBuf; int athleteId; char athleteName[BF_MAIN_NAME_SIZE]; char athleteSurName[BF_MAIN_NAME_SIZE]; /* Αρχικοποίηση του επιπέδου αρχείου μπλοκ */ BF_Init(); /* ’νοιγμα του αρχείου ATHLETES */ athletesFd = BF_OpenFile("ATHLETES"); if (athletesFd < 0) { BF_PrintError("Error in BF_OpenFile called on ATHLETES."); return; } /* Εκτύπωση όλων των αθλητών */ if (BF_GetFirstBlock(athletesFd, &athletesBlockNum, &athletesBlockBuf) != BFE_OK) BF_PrintError("Error in BF_GetFirstBlock called on ATHLETES."); printf("File ATHLETES contains the following athletes:\nID NAME\n--- ----------------------------\n"); do { int i; for (i = 0; i < BF_MAIN_MAX_NUM_OF_RECS; i++) { athleteId = * (int *) athletesBlockBuf; athletesBlockBuf += sizeof(int); strcpy(athleteName, athletesBlockBuf); athletesBlockBuf += BF_MAIN_NAME_SIZE; strcpy(athleteSurName, athletesBlockBuf); athletesBlockBuf += BF_MAIN_NAME_SIZE; if (strlen(athleteName) == 0) break; printf("%03d %s %s\n", athleteId, athleteName, athleteSurName); } BF_UnpinBlock(athletesFd, athletesBlockNum, FALSE); } while(BF_GetNextBlock(athletesFd, &athletesBlockNum, &athletesBlockBuf) == BFE_OK); printf("\n**********************\n\n"); /* Κλείσιμο του αρχείου ATHLETES. */ if (BF_CloseFile(athletesFd) < 0) BF_PrintError("Error in BF_CloseFile called on ATHLETES."); /* Σβήσιμο των τριών αρχείων */ if (BF_DestroyFile("EVENTS") != BFE_OK) BF_PrintError("Error in BF_DestroyFile called on EVENTS."); else printf("File EVENTS successfully destroyed.\n"); if (BF_DestroyFile("ATHLETES") != BFE_OK) BF_PrintError("Error in BF_DestroyFile called on ATHLETES."); else printf("File ATHLETES successfully destroyed.\n"); if (BF_DestroyFile("PARTICIPATIONS") != BFE_OK) BF_PrintError("Error in BF_DestroyFile called on PARTICIPATIONS."); else printf("File PARTICIPATIONS successfully destroyed.\n"); return 0; }