Write a C program you will implement a program to implement a small database and
ID: 3796102 • Letter: W
Question
Write a C program
you will implement a program to implement a small database and you will learn how to store it in a binary file.
You are to write a program that reads records of a database and stores it in a file (binary file).
The record on the following format.
Title Authors Publisher Price
Up to 60 characters Up to 50 characters Up to 50 characters Float.
The maximum number of records in the file is 40.
The program starts by reading the file “book.dat” to retrieve the existing database and stores it in an array of structs. A struct can hold all the information about one book.
If the file does not exist, then you have to create it
Think of how to open such a file, binary, create if not there, and you can append to it.
(Program output in BOLD. input --)
Then the program outputs the to the standard input the number of records in the file in the following format:
book.dat contains xx records
xx is the number of records and is less than or equal 40.
If the number of records is 40, it displays:
The file is full
The you proceed to the “inquiry” state.
If the number is less than 40, the program will prompt you to enter more books
Enter the name of the book
To stop entering, press enter at the beginning of a line
-- You enter the book title or a new line at the beginning of the line. If new line, it goes to “inquiry state”
Enter the author
-- You enter the author
Enter the publisher
--You enter the publisher
Enter the price
--You enter the price
If the number of books = the maximum number, go to Inquire state” else you go back to “enter the name of the book” until the user enters a new line at the beginning of the line, or you reach the maximum number of books.
Inquiry state
Your program prompts the user
Enter Q to end, or T or A to search for title and author
-- Enter Q, T, or A followed by newline (enter).
If you entered T, or A the program prompts you as follows
Enter the book title (in case of T) (Enter the book author in case of A).
-- You enter the book title/Author (you might want to get fancy and remove any white
spaces at the beginning or end.
The program responds as follows
If the book exists, it displays
Title: the title of the book
Author: the author(s) of the book
Price: the price (%f ”)
If the book does not exist, the program displays
No such book (followed by a new line).
Then it goes back to “Enter Q to end, or T or A .....”
-- If you enter Q
The program quits.
Things to consider
1. Try to make sense out of the binary file “book.dat” How the data stored in this file?
2. There is no need to use memory allocation for this, the array should have a maximum length of 40
3. Please do not use magic numbers.
Sample in.txt , out.txt, and book.dat to be read
in1.txt
https://wiki.eecs.yorku.ca/course_archive/2016-17/W/2031/_media/in1.txt
in2.txt
https://wiki.eecs.yorku.ca/course_archive/2016-17/W/2031/_media/in2.txt
out1.txt
https://wiki.eecs.yorku.ca/course_archive/2016-17/W/2031/_media/out1.txt
out2.txt
https://wiki.eecs.yorku.ca/course_archive/2016-17/W/2031/_media/out2.txt
book.dat (rename to book.dat)
https://wiki.eecs.yorku.ca/course_archive/2016-17/W/2031/_media/book_dat.txt
Explanation / Answer
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# define MAX 300 /* constant*/
struct addr {
char name[50] ;
char street[60] ;
char town[30] ;
char county[20] ;
char code[40] ;
}
list[MAX];
void menu_select(void);
void enter(void);
void find_free(void);
void inputs( char, char, int);
int main(void)
{
clrscr();
menu_select();
getch();
return 0;
}
void menu_select()
{
int choice;
printf("A. Enter a name ") ;
printf("B. Delete a name ") ;
printf("C. List the File ");
printf("D. Search ") ;
printf("E. Save the file ") ;
printf("F. Load the file ") ;
printf("G. Quit ") ;
do {
printf(" Enter your choice: ");
}
while(choice<0 || choice>7);
scanf("%d",&choice);
switch(choice)
{
case A: enter(); /* enter a new entry */
break;
// case B: del(); /* delete an entry */
// break;
// case C: show_list(); /* display the list */
// break;
// case D: search(); /* find an entry */
// break;
// case E: save(); /* save to disk */
// break;
// case F: load(); /* read from disk */
// break;
case 7: exit(0);
default:
printf("Invalid choice. Please enter again");
}
getch();
}
void enter()
{
int i;
for(;;)
{
i = find_free();
if(i<0)
{
printf("list full ");
return;
}
inputs("enter name: ", list[i].name,50);
if
(!*list[i].name) break;
inputs("enter street: ", list[i].street, 60);
inputs("enter town: ", list[i].town, 30);
inputs("enter county: ", list[i].county, 20);
inputs("enter Postal code: ", list[i].code, 40);
}
}
void find_free()
{
register int i;
for(i=0; i<MAX; i++)
if(!*list[i].name)
return i;
return -1;
}
void inputs( char *prompt , char *s , int count)
{
char str[255];
do
{
printf(prompt);
gets(str);
if
(strlen(str)>=count)
printf(" too long ");
}
while(strlen(str)>=count);
strcpy(s , str);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.