|
|
楼主 |
发表于 2013-8-3 12:08:01
|
显示全部楼层
- /*自定义插入函数*/
- void insert()
- {
- FILE *fp;
- int i, j, k, m = 0, snum;
- if ((fp = fopen("data", "ab+")) == NULL)
- {
- printf("不能打开文件!\n");
- return;
- }
- while(!feof(fp))
- if(fread(&comm[m], LEN, 1, fp) == 1)
- m++;
- if (m == 0)
- {
- printf("没有记录!\n");
- fclose(fp);
- return;
- }
- printf("请输入要插入记录的位置!\n");
- scanf("%d", &snum); /*输入要插入的位置*/
- for(i = 0; i < m; i++)
- if(snum == comm[i].num)
- break;
- for(j = m - 1; j > i; j--)
- comm[j + i] = comm[j]; /*从最后一条记录开始均向后移一位*/
- printf("Now please input the new information.\n");
- printf("编号:");
- scanf("%d", &comm[i + 1].num);
- for(k = 0; k < m; k++)
- if (comm[k].num == comm[i + 1].num && k != i + 1)
- {
- printf("该编号已经存在,按任意键继续!");
- getch();
- fclose(fp);
- return;
- }
- printf("商品名:");
- scanf("%s", comm[i + 1].name);
- printf("单价:");
- scanf("%lf", &comm[i + 1].price);
- printf("数量:");
- scanf("%lf", &comm[i + 1].count);
- comm[i + 1].total = comm[i + 1].price * comm[i + 1].count;
- if ((fp = fopen("data", "wb")) == NULL)
- {
- printf("不能打开文件!\n");
- return;
- }
- for(k = 0; k <= m; k++)
- if (fwrite(&comm[k], LEN, 1, fp) != 1) /*将修改后的记录写入磁盘文件中*/
- {
- printf("不能保存!");
- getch();
- }
- fclose(fp);
- }
- /*统计*/
- void total()
- {
- FILE *fp;
- int m = 0;
- if ((fp = fopen("data", "ab+")) == NULL)
- {
- printf("不能打开记录!\n");
- return;
- }
- while(!feof(fp))
- if(fread(&comm[m], LEN, 1, fp) == 1)
- m++; /*统计记录个数*/
- if (m == 0)
- {
- printf("没有记录!\n");
- fclose(fp);
- return;
- }
- printf("一共有 %d 条记录!\n", m); /*将统计的记录个数输出*/
- fclose(fp);
- }
复制代码 |
|