/* 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样例2
3星 · 超过75%的资源 需积分: 9 109 浏览量
2011-06-13
13:52:21
上传
评论
收藏 7KB RAR 举报
DRELi
- 粉丝: 1
- 资源: 2
最新资源
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
- 鱼群算法求解组环问题python源码+文档说明
- 基于决策优化的多波束测深测线规划模型MATLAB代码
- 课程设计-基于python实现的多目标优化算法求解带时间窗的车辆路径规划问题+源代码+文档说明+界面截图+pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈