#include #include #include #include struct infor { char ID[50]; char NAME[50]; float PRICE; long NUMBER; }g[1000],m[1000],x; struct inf { char id[50]; char name[50]; float price; long number; }c; FILE *p; void menu_s(); void input(); void output(); void check(); void updata(); void add(); void dele(); int len(); int len2(); int main() { system("cls");//清屏 int n; menu_s(); scanf("%d",&n); while(n) { system("cls"); switch(n) { case 1: input() ;break; case 2: output();break; case 3: updata();break; case 4: check();break; case 5: return 0; } getch(); menu_s(); scanf("%d",&n); } } void menu_s () { printf("----------------------------\n"); printf("***********主菜单***********\n"); printf("*********1:商品入库*********\n"); printf("*********2:商品出库*********\n"); printf("*********3:更新库存*********\n"); printf("*********4:查询库存*********\n"); printf("*********5:退出系统*********\n"); printf("*********请输入选项*********\n"); printf("----------------------------\n"); } void input() { int i=0,s; char next[2]="y"; p=fopen("1.txt","wb+"); if(p==NULL) { printf("打开文件失败\n"); fclose(p); exit(0); } while(strcmp(next,"y")==0||strcmp(next,"Y")==0) { printf("\n请输入商品编号\n"); scanf("%s",g[i].ID ); printf("\n请输入商品名称\n"); scanf("%s",&g[i].NAME ); printf("\n请输入商品价格\n"); scanf("%f",&g[i].PRICE ); printf("\n请输入商品数量\n"); scanf("%d",&g[i].NUMBER ); fwrite(&g[i],sizeof(struct infor),1,p);/*向文件写入数据*/ printf("\n继续输入(y(Y)/n(N))\n"); fflush(stdin);//清空缓存区。 i++; scanf("%s",next); } fclose(p); } int len()//读取当前文件长度 { int z=0; p=fopen("1.txt","r+"); if(p==NULL) { printf("打开文件失败\n"); exit(1); } rewind(p);//将文件指针移至文件开头。 while(!feof(p)) { if((fread(&g[z],sizeof(struct infor),1,p))==1); z++; } fclose(p); return z-1; } int len2()//读取当前文件长度 { int z=0; FILE *fp; fp=fopen("3.txt","r+"); if(fp==NULL) { printf("打开文件失败\n"); exit(1); } rewind(fp);//将文件指针移至文件开头。 while(!feof(fp)) { if((fread(&c,sizeof(struct inf),1,fp))==1); z++; } fclose(fp); return z; } void check() { int i,s=0; long length; p=fopen("1.txt","rb+"); if(p==NULL) { printf("打开文件失败\n"); exit(1); } printf("\n----------------------------------------------------\n商品列表\n----------------------------------------------------\n"); printf("编号\t\t品名\t\t价格\t\t数量\n"); rewind(p);//将文件指针移至文件开头。 s=len(); rewind(p); fread(g,sizeof(struct infor),s,p); for(i=0;i