Library management system (freshman C language homework includes main structure, file operation, such as data modification, query, deletion, etc.)

Posted by KyleVA on Sat, 29 Jan 2022 19:40:28 +0100

Library management system

1. Preface

At the University, our C language course has arranged a big assignment, which requires a management system. The whole system can be designed as data insertion module, data modification module, data deletion module, data display module and data query module. All kinds of information management systems are essentially not much different, and the operation of data is the same.

2. Functions and related functions

2.1 construction of menu interface

Two interface functions are established to build the home page. Here, two header files are used
#include<conio.h>
The data input and output functions through the console are defined
#include<windows.h>
Used to adjust the box size and background color

#include<conio.h>
#include<windows.h>
void Menu1()
{

	system("mode con cols=54 lines=30");
	system("color F2");
	printf("**********Welcome****University Library Management System***********\n");

}//Title Function 1

void Menu2()
{
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t1.Input books\t\t\n");
	printf("\t\t2.Show books\t\t\n");
	printf("\t\t3.Query books\t\t\n");
	printf("\t\t4.Revise books\t\t\n");
	printf("\t\t5.Delete book\t\t\n");
	printf("\t\t0.sign out\t\t\n");
	printf("\t\t Enter your action:");

}//Title Function 2

2.2 function 1: input books

Set a WriteToFile() function, use the scanf function to input the data to the structure, and use the fwrite function to write the structure data into the binary file "book1.dat". Use the while function to enter repeatedly.

2.3 function 2: display books

Use the function fread() to read the data out of the binary file

while (!feof(fp))
	{
		if (fread(&stu, LEN, 1, fp))
		{
			printf("%10s\t%8s\t\t%5s\n", stu.id, stu.name, stu.author);
		}
	}

At this time, the use of if statement can effectively avoid the repeated reading of structure data.

2.4 function 3: query books

At this time, use the strcmp function to "search" the book ID, read it out with the fread function, and then read it out with the printf function.

2.5 function 4: modify books

At this time, the strcmp function is used to "retrieve" the book ID, and all the data in its structure are re entered to achieve the purpose of modification. At this time, the fseek function has the effect of determining the file pointer.

2.6 function 5 delete books

Principle of deleting books: recreate a temporary file, copy all contents except the old file to the temporary file, delete the old file with the remove function, and rename the temporary file to the name of the old file with the rename function. Achieve the purpose of deletion

3. Precautions

(1) : you can use the linked list to simplify the program
(2) : you should try your best
Expand the functions of the management system
(3) : I use VS2017 to write programs. Different versions may be incompatible
(4) : main() uses the do while and switch functions to achieve the effect of the home menu

4. Source program

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define LEN sizeof(book)

typedef struct book //Confirm structure
{

	char id[8];
	char name[31];
	char author[27];
	char publish[31];
	double price;

}book;

void Menu1();//Title Function

void Menu2();//Menu function

void WriteToFile();//Book information input function

void ReadFromFile();//Functions that display all information

void QueryFile();//Query function of books

void ModifyFile();//Book modification function

void DeletFile();//Function to delete data

int main()
{
	int select;
	
	do 
	{
		Menu1();
		Menu2();
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			WriteToFile();
			break;

		case 2:
			ReadFromFile();
			break;

		case 3:
			QueryFile();
			break;

		case 4:
			ModifyFile();
			break;

		case 5:
			DeletFile();
			break;

		default:
			printf("Exit the program!");
			exit(0);
			break;
		}
	}while ((select == 1 || select == 2)||(select == 3|| select == 4)||( select == 5));

	return 0;
}//Use the switch function to select the menu

void Menu1()
{

	system("mode con cols=54 lines=30");
	system("color F2");
	printf("**********Welcome to the library management system of Changsha University of technology***********\n");

}//Title Function 1

void Menu2()
{
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t1.Input books\t\t\n");
	printf("\t\t2.Show books\t\t\n");
	printf("\t\t3.Query books\t\t\n");
	printf("\t\t4.Revise books\t\t\n");
	printf("\t\t5.Delete book\t\t\n");
	printf("\t\t0.sign out\t\t\n");
	printf("\t\t Enter your action:");

}//Title Function 2

void WriteToFile()
{
	FILE *fp = NULL;
	book stu;
	char flag = 'y';
	fp = fopen("book1.dat", "ab+");//Open file

	if (fp == NULL)
	{
		printf("File opening failed!\n");
		exit(1);//1 means to exit the program in the wrong way
	}

	while ((flag == 'y' || flag == 'Y'))
	{
		system("cls");
		Menu1();

		printf("Please enter a Book id: ");
		scanf("%s", stu.id);

		printf("Please enter the title of the book:");
		scanf("%s", stu.name);

		printf("Please enter the author of the book:");
		scanf("%s", &stu.author);

		printf("Please enter the publisher:");
		scanf("%s", &stu.publish);

		printf("Please enter price:");
		scanf("%lf", &stu.price);


		fwrite(&stu, LEN, 1, fp);
		fflush(stdin);

		printf("Continue typing? To continue, please enter y or Y: ");
		getchar();
		scanf("%c", &flag);
	}
	
	fclose(fp);//Close file
	return;
}//Functions added to books

void ReadFromFile()
{
	system("cls");
	Menu1();
	FILE *fp = NULL;
	book stu;
	fp = fopen("book1.dat", "rb");

	if (fp == NULL)
	{
		printf("File open failed");
		exit(1);
	}
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("         id\t\t title\t\t author\n");

	fseek(fp, 0, SEEK_SET);

	while (!feof(fp))
	{
		if (fread(&stu, LEN, 1, fp))
		{
			printf("%10s\t%8s\t\t%5s\n", stu.id, stu.name, stu.author);
		}
	}

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	system("pause");
	system("cls");
	fclose(fp);
	return;
}

void QueryFile()
{
	system("cls");
	Menu1();
	book stu;
	char x[8];
	int flag = 0;
	FILE *fp;

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t Please enter a Book id:");
	scanf("%s", x);
	printf("  ID  title    author     press     Price\n");

	fp = fopen("book1.dat", "rb");

	if (fp == NULL)
	{
		printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		printf("error\n");
		printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		return;
	}
	
	fseek(fp, 0, SEEK_SET);
	while (fread(&stu, LEN, 1, fp))
	{

		if (strcmp(x, stu.id) == 0)
		{
			printf("%3s  %5s  %5s    %10s %5.2lf\n", stu.id, stu.name, stu.author, stu.publish, stu.price);
			flag = 1;
		}

		if (flag = 0)
		{
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			printf("No book information");
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		}
	}

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	fclose(fp);
	system("pause");
	system("cls");
	return;
}

void ModifyFile()
{
	system("cls");
	Menu1();
	book stu;
	FILE *fp;
	char x[8];

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("Please enter a Book id:");
	scanf("%s", x);

	fp = fopen("book1.dat", "rb+");

	if (fp == NULL)
	{
		printf("File open failed");
		exit(1);
	}

	fseek(fp, 0, SEEK_SET);
	while (fread(&stu, LEN, 1, fp))
	{

		if (strcmp(x, stu.id) == 0)
		{
			printf("Please re-enter the book id:   ");
			scanf("%s", stu.id);

			printf("Please re-enter the title of the book:    ");
			scanf("%s", stu.name);

			printf("Please re-enter the book author  : ");
			scanf("%s", &stu.author);

			printf("Please re-enter the book publishing house  : ");
			scanf("%s", &stu.publish);

			printf("Please re-enter the book price :   ");
			scanf("%lf", &stu.price);
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			fflush(stdin);
			fseek(fp, 0-LEN, SEEK_CUR);
			fwrite(&stu, LEN, 1, fp);
			fclose(fp);
		}

		if (feof(fp))
		{
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			printf("No book information");
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		}

	}

	system("pause");
	system("cls");
	return;
}

void DeletFile()
{
	system("cls");
	Menu1();
	book s;
	FILE* fp;
	char a[10];
	fp = fopen("book1.dat", "rb+");

	if (fp == NULL)
	{
		printf("Error opening file!!!\n");
		exit(1);
	}

	printf("\n Please enter a Book ID: ");
	scanf("%s", a);
	printf("\n\t\t\t Deleted successfully\n");

	fseek(fp, 0, SEEK_SET);
	FILE* fp1;
	fp1 = fopen("linshi.dat", "ab+");//Read write create a new temporary file

	while (fread(&s, LEN, 1, fp))//Read a node from the original file
	{
		if (strcmp(a, s.id) != 0)//Not the content to delete
		{
			fwrite(&s, LEN, 1, fp1);
		}
	}

	fclose(fp);
	fclose(fp1);
	remove("book1.dat");//delete original file
	rename("linshi.dat", "book1.dat");//Rename to original file

	fflush(stdin);
	system("pause");
	system("cls");
	return;
}