#include #include #include #include "types.h" #include "suppliers.h" #include "hw5.h" main() { char line[80]; char ch; int fd, done; fd = open("suppliers.dat", O_RDWR | O_CREAT, 0600); done = FALSE; do { print_menu(); printf("Type in your option: "); getline(line,sizeof(line)); sscanf(line,"%c",&ch); switch (ch) { case 'm': makenull_supp(fd); printf("\n"); break; case 'i': ins_supplier(fd); printf("\n"); break; case 'd': del_supplier(fd); printf("\n"); break; case 'f': find_supplier(fd); printf("\n"); break; case 'p': print_suppliers(fd); printf("Press RETURN to continue"); getchar(); printf("\n"); break; case 'q': case 'Q': done = TRUE; break; default: printf("Type in option again\n"); break; } } while (!done); close(fd); } void print_menu() { printf("\n"); printf("m. INITIALIZE SUPPLIER SET \n"); printf("i. INSERT SUPPLIER \n"); printf("d. DELETE SUPPLIER \n"); printf("f. FIND SUPPLIER \n"); printf("p. PRINT SUPPLIERS \n"); printf("q. QUIT\n"); printf("\n"); } void ins_supplier(int fd) { struct supp_element_type x; char line[80]; printf("Type in Supplier Number: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); printf("Type in Supplier Name: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sname); if (insert_supp(x,fd) == TRUE) printf("Supplier %10s has been inserted", x.sno); else printf("Supplier %10s already exists", x.sno); } void find_supplier(int fd) { struct supp_element_type x; char line[80]; printf("Type in SNO to be searched: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); if (member_supp(&x,fd) == TRUE) printf("The name of supplier %10s is %s ", x.sno, x.sname); else printf("Supplier %10s does not exist", x.sno); } void del_supplier(int fd) { struct supp_element_type x; char line[80]; printf("Type in SNO to be deleted: "); getline(line,sizeof(line)); sscanf(line,"%s",x.sno); if (delete_supp(x,fd) == TRUE) printf("Supplier %10s has been deleted", x.sno); else printf("Supplier %10s does not exist", x.sno); } int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; }