Code:
#define LinesPerScreen 12
#include <stdio.h>
#include <process.h>
#include <conio.h>
#include <string.h>
char lend_date[20];
char borrower_name[15];
char item_name[30];
char return_date[10];
void display_item(void);
void delete_item(void);
void add_item(void);
void update_item(void);
void main()
{
int reply = 0;
int items = 5;
while (reply < items)
{
printf("n\n MAIN MENU");
printf("\n\n1. Displaying the list of lend items.");
printf("\n2. Deleting an item from the list.");
printf("\n3. Adding an item to the list.");
printf("\n4. Updating an item");
printf("\n5. (or 6, ..., etc.) EXIT the program.");
printf("\n");
printf("\nTo terminate choose the option 5, or higher!");
printf("\Please choose the items to run (e.g 1, ..., 5) ");
scanf("%d", &reply);
if (reply == 1)
{
display_item();
}
if (reply == 2)
{
delete_item();
}
if (reply == 3);
{
add_item();
}
if (reply == 4);
{
update_item();
}
if (reply >= 5)
{
printf ("\n\nprogram terminated... ");
}
if (reply < 1)
{
printf ("\nChoice %d is out of range (retype 1, ..., 5).", reply);
}
}
printf("\n");
printf("\nThanks for using this program! ");
printf("Good-bye!");
printf("\n");
system("pause");
}
void display_item()
{
FILE *display_file;
char content_in[60];
int item_no = 0;
int line = 0;
system("cls");
printf("\nThis is the list of lend items ");
printf("\n");
display_file = open ("borrowers.txt", "r");
fgets(content_in, 60, display_file);
while(!feof(display_file))
{
item_no++;
line++;
if(fgets (content_in, 60, display_file))
printf("\Item No. %d: %s", item_no, content_in);
if(line == LinesPerScreen)
{
printf("\n");
system("pause");
system("cls");
line = 0;
}
}
printf("\n\nThis is the end of list");
fclose(display_file);
}
void delete_item()
{
FILE *origin_file;
FILE *temp_file;
char content_in[60];
int item_no = 0;
int line = 1;
system("cls");
printf("\n Deleting an item");
printf("\n");
if((origin_file = fopen("borrowers.txt", "r"))== NULL)
{
perror("Unable to open file ");
getch();
}
temp_file = fopen("temp.txt", "w+");
printf("Please enter an item number you want to delete: ");
scanf("%d", &item_no);
while(!feof(origin_file))
{
if(fgets(content_in, sizeof(content_in), origin_file))
if(line != item_no+1)
fprintf(temp_file, content_in);
line++;
}
fclose(origin_file);
fclose(temp_file);
system("copy temp.txt borrowers.txt");
system("del temp.txt");
}
void add_item()
{
FILE *add_file;
system("cls");
printf("\n Adding an item ");
printf("\n");
add_file = fopen ("borrows.txt", "a");
printf("\nPlease enter date of book lending(in this format: 24/05/03): ");
gets(lend_date);
printf("\nPlease enter name of borrower(e.g John): ");
gets(borrower_name);
printf("\nPlease enter name of lent(e.g Networking): ");
gets(item_name);
printf("\nPlease enter date of book return(in this format: 14/06/03): ");
gets(return_date);
fprintf(add_file, "\n%s %s %s %s",
lend_date, borrower_name, item_name, return_date);
fclose(add_file);
printf("Adding finished");
}
void update_item()
{
FILE *origin_file;
FILE *temp_file;
char content_in[60];
int item_no = 0;
int line = 0;
system("cls");
printf("\n Updating an item");
printf("\n");
origin_file = fopen("borrowers.txt", "r");
temp_file = fopen("temp.txt", "w+");
printf("Please enter an item you want to update: ");
scanf("%d", &item_no);
printf("\Please enter the information for this record...\n");
fflush(stdin);
printf("\nPlease enter date of book lending(in this format: 24/05/03: ");
gets(lend_date);
printf("\nPlease enter name of borrower(e.g. John): ");
gets(borrower_name);
printf("\nPlease enter name of book lent(e.g Networking): ");
gets(item_name);
printf("\Please enter date of book return(in this format: 14/06/03): ");
gets(return_date);
for (line = 1; line < item_no+1; line++)
{
fgets(content_in, 60, origin_file);
fprintf(temp_file, content_in);
}
fprintf(temp_file, "%s %s %s %s\n",
lend_date, borrower_name, item_name, return_date);
fgets(content_in, 60, origin_file);
while (!feof(origin_file))
{
fgets(content_in, 60, origin_file);
fprintf(temp_file, content_in);
}
fclose(origin_file);
fclose(temp_file);
system("copy temp.txt borrowers.txt");
system("del temp.txt");
}