⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbms2.c

📁 一个小型的用C语言编写的数据库实现
💻 C
字号:
/* dbms2.cpp :  Database Manage System (2) -- Add & View Record.  */


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

#define MAX_SIZE 50		/*表中所含字段最大长度*/
#define FILE_NAME_LENGTH 15		/*文件名最大长度*/
#define FIELD_NAME_LENGTH 15	/*字段名最大长度*/	

typedef struct {
  char sFieldName[FIELD_NAME_LENGTH];
  char sType[8];
  int iSize;
  char bKey;
  char bNullFlag;
  char bValidFlag;
  } TableMode,*PTableMode;

void main()
{
   FILE *fp,*tempfp;
   int tempi,tempi1,tempiNum,iNum,jNum;  /*iNum is the number of the Field,and jNum is Record */
   char tempc,temps[FILE_NAME_LENGTH],*temps1;
   char recordset[2*MAX_SIZE][FILE_NAME_LENGTH];
   TableMode FieldSet[MAX_SIZE];
   char dbf[FILE_NAME_LENGTH],datf[FILE_NAME_LENGTH],tname[FILE_NAME_LENGTH],temptn[FILE_NAME_LENGTH];
   int bFound;  /*found the table */

   printf("\n*********** Database Manage System Design (2)- Add & View Record ************ ");
   printf("\n      *********** ECUST by LZH ************ ");
   while(1){
	   printf("\n");
	   printf("\n");
	   printf("\n");
	   printf("\n        ********************************* ");
	   printf("\n");
	   printf("\n              0 -Add New Record");
	   printf("\n              1 -View Records");
	   printf("\n              2 -Exit");
	   printf("\n");
	   printf("\n        ********************************* ");
	   printf("\nEnter your choice: ");
	   tempc=getch();
	   while(tempc!='0'&&tempc!='1'&&tempc!='2')tempc=getch();
	   printf("%c",tempc);

	  switch(tempc){
	  case '0':
		   printf("\n\n******** Add New Record ********");
		   printf("\ninput Data file (.dat) to Save New Record (MAX char[15]): ");
		   scanf("%15s",datf);
		   printf("\ninput the Database File Name(.dbf) which the Table Mode is in : ");
		   scanf("%15s",dbf);
		   if((fp=fopen(dbf,"rb")) == NULL ){
				printf("\nFile Not Exist or Bad File!\n" );
				break;
		   }
		   printf("\ninput the Table Name which the Table Mode is in : ");
		   scanf("%15s",temptn);
		   /* find Table Mode in .dbf*/
		   while(!feof(fp)){
			   bFound=0;
			   tempi=fread(&tempc,sizeof(char),1,fp);
			   if(!tempi)break;
			   fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
			   fread(&iNum,sizeof(int),1,fp);
			   fread(FieldSet,sizeof(TableMode),iNum,fp);
		       if(strcmp(tname,temptn)==0){
			       printf("\nFound! Table Fields are as following:");
				   printf("\nFieldName\tType\tSize\tKey\tNull");
				   for(tempi=0;tempi<iNum;tempi++)
				       printf("\n%s\t%s\t%d\t%c\t%c",FieldSet[tempi].sFieldName,FieldSet[tempi].sType,
					       FieldSet[tempi].iSize,FieldSet[tempi].bKey,FieldSet[tempi].bNullFlag);
					   bFound=1;
					   break;  /*break while*/
			   }
		   }
		   fclose(fp);
		   if(!bFound){
			   printf("\nNo such a table in %s",dbf);
			   break;
		   }

		   if((fp=fopen(datf,"ab+")) == NULL ){
				printf("\nCan't Creat Data File %s!",datf);
				break;
		   }

		   jNum=0;
		   printf("\n\nAdd New Record according to below Table Mode.");
		   do{
			   for(tempi=0;tempi<iNum;tempi++){
					printf("\ninput %s: ",FieldSet[tempi].sFieldName);
					scanf("%15s",temps);
					strcpy(&recordset[jNum*iNum+tempi][0],temps);
			   }
			   jNum++;
			   printf("\nAdded A New Record Successfully!");
			   printf("\nContinue Add Another Record? (y -Yes, n -No): ");
		       tempc=getch();
		       while(tempc!='y'&&tempc!='n')tempc=getch();
		       printf("%c",tempc);
		   }while(tempc=='y');

		   temps1=(char*)malloc(sizeof(char)*jNum);
		   for(tempi=0;tempi<jNum;tempi++)temps1[tempi]='V'; /*  V means validation */

		   /* write .dat */
		   fwrite("~",sizeof(char),1,fp);
		   fwrite(tname,sizeof(char),FILE_NAME_LENGTH,fp);
		   fwrite(&jNum,sizeof(int),1,fp);
		   fwrite(&iNum,sizeof(int),1,fp);
		   fwrite(temps1,sizeof(char),jNum,fp);
		   fwrite(recordset,sizeof(char),FILE_NAME_LENGTH*sizeof(char)*jNum*iNum,fp);
		   fclose(fp);
		   free(temps1);
		   break;
	  case '1':
	  	   printf("\n\n******** View Records ********");
		   printf("\ninput Database File Name(.dbf) : ");
		   scanf("%15s",dbf);
		   if((tempfp=fopen(dbf,"rb")) == NULL ){
				printf("\nFile Not Exist or Bad File!\n" );
				break;
		   }
		   printf("\ninput Data File Name(.dat) : ");
		   scanf("%15s",datf);
		   if((fp=fopen(datf,"rb"))== NULL ){
			   printf("\nFile Not Exist or Bad File!\n" );
			   break;
		   }

		   printf("\nTables in %s are as following:",datf);
		   while(!feof(fp)){
			   tempi=fread(&tempc,sizeof(char),1,fp);
			   if(!tempi)break;
			   if(tempc!='~'){
				   printf("\n%s Data Format NOT corroct!",datf);
				   exit(1);
			   }
			   fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
			   printf("\n%s",tname);
			   fread(&jNum,sizeof(int),1,fp);
			   fread(&iNum,sizeof(int),1,fp);
			   temps1=(char*)malloc(sizeof(char)*jNum);
		       fread(temps1,sizeof(char),jNum,fp);
			   fread(recordset,sizeof(char),FILE_NAME_LENGTH*jNum*iNum,fp);
			   free(temps1);
		   }
		   printf("\n");
		   printf("\ninput Table Name to view it's Records: ");
		   scanf("%15s",temptn);

		   /*find table fields in .dbf*/
		   while(!feof(tempfp)){
			   bFound=0;
			   tempi=fread(&tempc,sizeof(char),1,tempfp);
			   if(!tempi)break;
			   fread(tname,sizeof(char),FILE_NAME_LENGTH,tempfp);
			   fread(&tempiNum,sizeof(int),1,tempfp);
			   fread(FieldSet,sizeof(TableMode),tempiNum,tempfp);
		       if(strcmp(tname,temptn)==0){
					   bFound=1;
					   break;  /*break while*/
			   }
		   }
		   fclose(tempfp);
		   if(!bFound){
			   printf("\nNo such a table in %s",dbf);
			   break;
		   }
			/*   end find */

		   rewind(fp);
		   bFound=0;
		   while(!feof(fp)){
			   tempi=fread(&tempc,sizeof(char),1,fp);
			   if(!tempi)break;
			   fread(tname,sizeof(char),FILE_NAME_LENGTH,fp);
			   fread(&jNum,sizeof(int),1,fp);
			   fread(&iNum,sizeof(int),1,fp);
			   temps1=(char*)malloc(sizeof(char)*jNum);
		       fread(temps1,sizeof(char),jNum,fp);
			   
			   if(strcmp(tname,temptn)==0){
				   printf("\nTable Records are as following:\n");
				   for(tempi=0;tempi<tempiNum;tempi++)
					   printf("%s\t",FieldSet[tempi].sFieldName);
				   for(tempi=0;tempi<jNum;tempi++){
					   printf("\n");
					   for(tempi1=0;tempi1<iNum;tempi1++){
						   fread(temps,sizeof(char),FILE_NAME_LENGTH,fp);
						   printf("%s\t",temps);
					   }
				   }
				   bFound=1;
			   }
			   else
				   fread(recordset,sizeof(char),FILE_NAME_LENGTH*jNum*iNum,fp);
			   free(temps1);
		   }
		   if(!bFound)printf("\nNo such a table in %s",dbf);
		   break;
	  case '2':
		   exit(0);
	   }	/*end case*/
   }	/* end do while*/
}	/*end program*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -