修改如下,供参考:
#include<stdio.h>
#include <malloc.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<windows.h>
typedef struct Equ
{
char num [10];
char kind[12];
char name[18];
char price[20]; //价格也是字符型?
char buy_time[20];
char lab[8];
struct Equ*next;
}Equ;
Equ stu[100];
int LEN = sizeof(struct Equ);
void in(Equ stu[])
{
int i,m=0;
char ch[2]="y";
FILE *fp;
if((fp=fopen("data.txt","r"))==NULL){
printf("can not open file!\n");
return;
}
while(1){
if(fread(&stu[m],LEN,1,fp)!=1) break;
m++;
}
fclose(fp);
while(ch[0] == 'Y' || ch[0] == 'y'){
printf("Input num:");
while(1){
scanf("%s",stu[m].num);//输入设备编号
for(i=0;i<m;i++){
if(strcmp(stu[i].num,stu[m].num)==0){
printf("此编号已存在,请重新输入编号:");
break;
}
}
if(i==m)break;
}
printf("kind:");
getchar();
scanf("%s",stu[m].kind);//输入设备种类
printf("name:");
getchar();
scanf("%s",stu[m].name);//输入设备名称
printf("price:");
getchar();
scanf("%s",stu[m].price);//输入设备价格
printf("buy_time:");
getchar();
scanf("%s",stu[m].buy_time);//输入购买时间
printf("lab:");
getchar();
scanf("%s",stu[m].lab); //输入所属实验室
m++;
printf("please input again(y/n):");
getchar();
scanf("%s",ch);
}
if((fp=fopen("data.txt","w"))==NULL){
printf("file write fail!\n");
return;
}
for(i=0;i<m;i++)
fwrite(&stu[i],LEN,1,fp);
fclose(fp);
printf("OK!\n");
}
供参考:
scanf("%s",ch);
while(ch[0] == 'Y' || ch[0] == 'y')
{
.......
}
printf("please input(y/n):");
getchar();
scanf("%s",ch);
while(ch[0] == 'Y' || ch[0] == 'y')
{
.......
}