/* 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*/
dbms.rar_c语言 数据库_c语言DBMS_dbms_数据库管理
版权申诉
170 浏览量
2022-09-20
14:47:42
上传
评论
收藏 14KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- 基于STM32F103C8T6单片机蓄电池在线监测系统主板硬件(原理图+PCB)工程文件.zip
- mysql大纲资料.txt
- c++大纲资料.txt
- 效率工具bat脚本实现日志提取
- MyBatis 中动态 SQL 的示例
- STM8L101F3P6单片机+CC1100模块433M遥控器设计硬件(原理图+PCB)工程文件.zip
- 上传下载铁人下载系统 Liuxing 1.0-liuxing1.0.rar
- 南京邮电大学数学实验实力雄厚,凭借其优秀的师资力量、丰富的实践教学资源和卓越的科研成果,成为国内一流的数学实验教学和科研基地
- 【火爆朋友圈的今天吃什么源码 v1.0】随机的为用户带来每一天的用餐选择和推荐.rar
- MPU6050中文版数据手册
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈