#include #include #include struct infor { char num[50]; char name[50]; float price; }; struct infor g; FILE *part1; void menu_s(); void input(); void check(); 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: //sales();break; case 6: return 0; } getch(); menu_s(); scanf("%d",&n); } } void menu_s () { printf("***********主菜单************\n"); printf("*********1:商品入库*********\n"); printf("*********2:商品出库*********\n"); printf("*********3:更新库存*********\n"); printf("*********4:查询库存*********\n"); printf("*********5:销售统计*********\n"); printf("*********6:退出系统*********\n"); printf("*********请输入选项*********\n"); printf("----------------------------\n"); } void input() { char next='y'; part1=fopen("storg","rb+"); if(part1==NULL) { part1=fopen("storg","wb+"); if(part1==NULL) { printf("打开文件失败\n"); exit(0); } } //fseek(part1,0,SEEK_END); while(next=='y') { printf("\n请输入商品编号\n"); gets(g.num ); printf("\n请输入商品名称\n"); gets(g.name ); printf("\n请输入商品价格\n"); scanf("%lf",&g.price ); fprintf(part1,"%10s%10s%10s",g.num ,g.name ,g.price );/*向文件写入数据*/ printf("\n继续输入(y/n)\n"); fflush(stdin);//清空缓存区。 next=getch(); } fclose(part1); } void check() { part1=fopen("storg","rb+"); if(part1==NULL) { printf("打开文件失败\n"); exit(1); } rewind(part1); printf("\n\n商品列表\n****************\n"); printf("id name price\n"); while(!feof(part1)) { fscanf(part1, "%10s%10s%10s",g.num ,g.name ,&g.price); //从文件读出数据到结构体 printf("%-10s %-10s %-10f\n",g.num ,g.name ,g.price); } fclose(part1); }