机房存取操作
内容解上一篇机房管理系统
实现文件数据的连续访问
文件存取
void cun(void){//将数据存储到文件
FILE *fptr;
fptr = fopen("机房数据.txt", "w");
int i = 0;
for ( i = 1; i < 10; i++)
{
if (jifang[i].id == 0)
{ char x=0;
fprintf(fptr,"%d\t", jifang[i].id);
fprintf(fptr,"%s\t", x);
fprintf(fptr,"%s\t", x);
fprintf(fptr,"%s\t", x);
fprintf(fptr,"%s\t", x);
fprintf(fptr,"%s\n", x);
}
if (jifang[i].id != 0)
{
fprintf(fptr,"%d\t", jifang[i].id);
fprintf(fptr,"%s\t", jifang[i].name);
fprintf(fptr,"%s\t", jifang[i].banji);
fprintf(fptr,"%s\t", jifang[i].kecheng);
fprintf(fptr,"%s\t", jifang[i].xingqi);
fprintf(fptr,"%s\n", jifang[i].jiangci);
}
}
fclose(fptr);
}
文件读入到结构体数组
void qu(void){//读取文本数据
FILE *fptr;
fptr = fopen("机房数据.txt", "r");
int i = 0;
for ( i = 1; i < 10; i++)
{
fscanf(fptr,"%d[^\t]",&jifang[i].id);
fscanf(fptr,"%s[^\t]", jifang[i].name);
fscanf(fptr,"%s[^\t]", jifang[i].banji);
fscanf(fptr,"%s[^\t]", jifang[i].kecheng);
fscanf(fptr,"%s[^\t]", jifang[i].xingqi);
fscanf(fptr,"%s[^\n]", jifang[i].jiangci);
}
}
主函数
int main(void)
{
unsigned char num;
qu();
while (1){
menu(); //菜单界面
scanf("%d", &num); //获取操作命令
if (num >= 0 && num <= 5){ //判断操作命令是否有效
switch (num) //对有效命令做出响应
{
case 0: cun(); printf("退出成功!\r\n"); return 0; //退出
case 1: look(); break; //查看
case 2: add();break; //添加
case 3: Delete(); break; //删除
case 4: tianjia (); break; //修改
case 5: zhanshi(); break; //展示
}
}
else{ //提示错误信息
printf("输入错误,请重新输入\r\n");
}
}
return 0;
}