-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveBook.c
86 lines (61 loc) · 1.85 KB
/
removeBook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include "book.h"
#include "member.h"
#include "admin.h"
#include "menu.h"
#include "dataTypes.h"
#include "utilities.h"
void removeBook() {
SetConsoleTitle("Library System > Book Menu > Removing Book");
// declaring a variable of bookDataType so if found, it can be used
// no need for array
// writing into files will be instantly
bookDataType book;
do{
system("cls");
char choiceISBN[20];
printf("Enter the book's ISBN to remove : ");
gets(choiceISBN);
FILE * books = fopen("books.bin", "r");
// failure check
if(books==NULL) {
perror("\a\nError");
getche();
return;
}
FILE * booksTemp = fopen("booksTemp.bin", "w");
// failure check
if(booksTemp==NULL) {
perror("\a\nError");
getche();
return;
}
bool found = false;
// read from original file
while( fread(&book,sizeof(bookDataType),1,books) ) {
if( strcmp(book.ISBN , choiceISBN)!=0 )
// write into temporary file
fwrite(&book,sizeof(bookDataType),1,booksTemp);
else
found = true;
}
fclose(books);
fclose(booksTemp);
remove("books.bin");
rename("booksTemp.bin" , "books.bin");
showBookDetails(choiceISBN);
if(found)
acpMsg("Book removed successfully");
else
errMsg("No such ISBN for a book to remove");
}while( yesNoRequest("remove another book")==true );
fflush(stdin);
bookMenu();
}