鱼C论坛

 找回密码
 立即注册
查看: 4290|回复: 6

文件的输入输出问题

[复制链接]
发表于 2016-1-3 09:50:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
struct Student
{
        char name[20];
        int num;
        double score[3];
}stu[SIZE];
int main()
{
        FILE *p1,*p2,*p3,*p4;
        int i=0,n,j,t,a,b=0,c;
        if((p1=fopen("stud","wb"))==NULL)
        {
                printf("文件无法打开!\n");
                exit(0);
        }
        do
        {
                printf("***简单的成绩管理系统***\n");
                printf("\n");
                printf("           【请选择】\n");
                printf("【1】***添加一个学生的的记录\n");
                printf("【2】***查看一个学生的记录\n");
                printf("【3】***修改一个学生的记录\n");
                printf("【4】***浏览所有学生记录\n");
                scanf("%d",&n);
                while(n!=1&&n!=2&&n!=3&&n!=4)
                {
                        printf("输入错误,请重新输入:");
                        scanf("%d",&n);               
                }
                switch(n)
                {
                case 1:
                        if((p1=fopen("stud","ab"))==NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                                printf("请输入学生姓名:");
                                scanf("%s",stu[i].name);
                                printf("请输入该学生的学号:");       
                                scanf("%d",&stu[i].num);
                                printf("请输入该学生的高数成绩:");       
                                scanf("%lf",&stu[i].score[0]);
                                while(stu[i].score[0]<0||stu[i].score[0]>100)
                                {
                                        printf("高数成绩输入错误,请重新输入:");       
                                        scanf("%lf",&stu[i].score[0]);
                                }
                                printf("请输入该学生的英语成绩:");       
                                scanf("%lf",&stu[i].score[1]);
                                while(stu[i].score[1]<0||stu[i].score[1]>100)
                                {
                                        printf("英语成绩输入错误,请重新输入:");       
                                        scanf("%lf",&stu[i].score[1]);
                                }
                                printf("请输入该学生的C++成绩:");       
                                scanf("%lf",&stu[i].score[2]);
                                while(stu[i].score[2]<0||stu[i].score[2]>100)
                                {
                                        printf("C++成绩输入错误,请重新输入:");       
                                        scanf("%lf",&stu[i].score[2]);
                                }
                                fseek(p1,i*sizeof(struct Student),1);
                                if(fwrite(&stu[i],sizeof(struct Student),1,p1)!=1)
                                {
                                        printf("输入错误!\n");
                                }
                                i++;
                                printf("输入成功\n");
                                fclose(p1);
                                break;
                case 2:
                        if((p2=fopen("stud","rb"))==NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        printf("请输入要查看的学生的学号:");
                        scanf("%d",&j);
                        t=i;
                        while(!feof(p2))
                        {
                                if(fread(&stu[i],sizeof(struct Student),1,p2)!=-1)
                                {
                                        if(j==stu[i].num)
                                        {
                                                printf("该学生的姓名    : %s \n",stu[i].name);
                                                printf("该学生的高数成绩: %.1lf \n",stu[i].score[0]);
                                                printf("该学生的英语成绩: %.1lf \n",stu[i].score[1]);
                                                printf("该学生的C++成绩 : %.1lf \n",stu[i].score[2]);
                                        }
                                        else
                                        {
                                                b++;
                                        }
                                }
                        }
                        if(b==SIZE)
                        {
                                printf("该同学记录不存在!!\n");
                        }
                        i=t;
                        printf("\n");
                        fclose(p2);
                        break;
                case 3:
                        printf("请输入要修改学生的学号:");
                        scanf("%d",&j);
                        t=i;
                        for(i=0;i<SIZE;i++)
                        {
                                if(fread(&stu[i],sizeof(struct Student),1,p3)!=-1)
                                {
                                        if(stu[i].num==j)
                                        {
                                                printf("该学生姓名: %s \n",stu[i].name);
                                                printf("请重新输入该学生的高数成绩: ");
                                                scanf("%lf",&stu[i].score[0]);
                                                printf("请重新输入该学生的英语成绩: ");
                                                scanf("%lf",&stu[i].score[1]);
                                                printf("请重新输入该学生的C++成绩 : ");
                                                scanf("%lf",&stu[i].score[2]);
                                        }
                                }
                        }
                        if((p3=fopen("stud","rb"))==NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        if(fwrite(&stu[i],sizeof(struct Student),1,p3)!=1)
                        {
                                printf("输入错误!\n");
                        }
                        i=t;
                        printf("\n");
                        //fclose(p3);
                        break;
                case 4:
                        if((p4=fopen("stud","r"))==NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        t=i;
                        for(i=0;i<2;i++)
                        {
                                if(fread(&stu[i],sizeof(struct Student),1,p4)!=-1)
                                {
                                        printf("学生姓名  : %s \n",stu[i].name);
                                        printf("学生的学号: %d \n",stu[i].num);
                                        printf("高数成绩为: %.1lf \n",stu[i].score[0]);
                                        printf("英语成绩为: %.1lf \n",stu[i].score[1]);
                                        printf("C++成绩为 : %.1lf \n",stu[i].score[2]);       
                                        printf("\n");
                                       
                                }
                        }
                        i=t;
                        //fclose(p4);
                        break;
                default:printf("输入错误\n");
                       
                }
                printf("\n");
                printf("***请选择***\n");
                printf("1***返回主菜单\n");
                printf("0***退出程序\n");
                scanf("%d",&a);
                while(a!=1&&a!=0)
                {
                        printf("输入错误,请重新输入:");
                        scanf("%d",&a);
                }
               
        }while(a==1);
       
        //fclose(p2);
        //fclose(p3);
        //fclose(p4);
        return 0;
}
该程序我用case 1输入数据后,然后退出窗口,再打开时,选择case 2时,为什么数据全不见了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-1-3 10:45:07 | 显示全部楼层
不懂,帮顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-6 23:10:48 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE 100
  4. struct Student
  5. {
  6.         char name[20];
  7.         int num;
  8.         double score[3];
  9. }stu[SIZE];
  10. int main()
  11. {
  12.         FILE *p1, *p2, *p3, *p4;
  13.         int i = 0, n, j, t, a, b = 0, c;
  14.         if ((p1 = fopen("stud", "wb")) == NULL)
  15.         {
  16.                 printf("文件无法打开!\n");
  17.                 exit(0);
  18.         }
  19.         do
  20.         {
  21.                 printf("***简单的成绩管理系统***\n");
  22.                 printf("\n");
  23.                 printf("           【请选择】\n");
  24.                 printf("【1】***添加一个学生的的记录\n");
  25.                 printf("【2】***查看一个学生的记录\n");
  26.                 printf("【3】***修改一个学生的记录\n");
  27.                 printf("【4】***浏览所有学生记录\n");
  28.                 scanf("%d", &n);
  29.                 while (n != 1 && n != 2 && n != 3 && n != 4)
  30.                 {
  31.                         printf("输入错误,请重新输入:");
  32.                         scanf("%d", &n);
  33.                 }
  34.                 switch (n)
  35.                 {
  36.                 case 1:
  37.                         if ((p1 = fopen("stud", "ab")) == NULL)
  38.                         {
  39.                                 printf("文件无法打开!\n");
  40.                                 exit(0);
  41.                         }
  42.                         printf("请输入学生姓名:");
  43.                         scanf("%s", stu[i].name);
  44.                         printf("请输入该学生的学号:");
  45.                         scanf("%d", &stu[i].num);
  46.                         printf("请输入该学生的高数成绩:");
  47.                         scanf("%lf", &stu[i].score[0]);
  48.                         while (stu[i].score[0]<0 || stu[i].score[0]>100)
  49.                         {
  50.                                 printf("高数成绩输入错误,请重新输入:");
  51.                                 scanf("%lf", &stu[i].score[0]);
  52.                         }
  53.                         printf("请输入该学生的英语成绩:");
  54.                         scanf("%lf", &stu[i].score[1]);
  55.                         while (stu[i].score[1]<0 || stu[i].score[1]>100)
  56.                         {
  57.                                 printf("英语成绩输入错误,请重新输入:");
  58.                                 scanf("%lf", &stu[i].score[1]);
  59.                         }
  60.                         printf("请输入该学生的C++成绩:");
  61.                         scanf("%lf", &stu[i].score[2]);
  62.                         while (stu[i].score[2]<0 || stu[i].score[2]>100)
  63.                         {
  64.                                 printf("C++成绩输入错误,请重新输入:");
  65.                                 scanf("%lf", &stu[i].score[2]);
  66.                         }
  67.                         fseek(p1, i*sizeof(struct Student), 1);
  68.                         if (fwrite(&stu[i], sizeof(struct Student), 1, p1) != 1)
  69.                         {
  70.                                 printf("输入错误!\n");
  71.                         }
  72.                         i++;
  73.                         printf("输入成功\n");
  74.                         fclose(p1);
  75.                         break;
  76.                 case 2:
  77.                         if ((p2 = fopen("stud", "rb")) == NULL)
  78.                         {
  79.                                 printf("文件无法打开!\n");
  80.                                 exit(0);
  81.                         }
  82.                         printf("请输入要查看的学生的学号:");
  83.                         scanf("%d", &j);
  84.                         t = i;
  85.                         while (!feof(p2))
  86.                         {
  87.                                 if (fread(&stu[i], sizeof(struct Student), 1, p2) != -1)
  88.                                 {
  89.                                         if (j == stu[i].num)
  90.                                         {
  91.                                                 printf("该学生的姓名    : %s \n", stu[i].name);
  92.                                                 printf("该学生的高数成绩: %.1lf \n", stu[i].score[0]);
  93.                                                 printf("该学生的英语成绩: %.1lf \n", stu[i].score[1]);
  94.                                                 printf("该学生的C++成绩 : %.1lf \n", stu[i].score[2]);
  95.                                         }
  96.                                         else
  97.                                         {
  98.                                                 b++;
  99.                                         }
  100.                                 }
  101.                         }
  102.                         if (b == SIZE)
  103.                         {
  104.                                 printf("该同学记录不存在!!\n");
  105.                         }
  106.                         i = t;
  107.                         printf("\n");
  108.                         fclose(p2);
  109.                         break;
  110.                 case 3:
  111.                         printf("请输入要修改学生的学号:");
  112.                         scanf("%d", &j);
  113.                         t = i;
  114.                         for (i = 0; i<SIZE; i++)
  115.                         {
  116.                                 if ([color=Red]fread(&stu[i], sizeof(struct Student), 1, p3)[/color] != -1)
  117.                                 {
  118.                                         if (stu[i].num == j)
  119.                                         {
  120.                                                 printf("该学生姓名: %s \n", stu[i].name);
  121.                                                 printf("请重新输入该学生的高数成绩: ");
  122.                                                 scanf("%lf", &stu[i].score[0]);
  123.                                                 printf("请重新输入该学生的英语成绩: ");
  124.                                                 scanf("%lf", &stu[i].score[1]);
  125.                                                 printf("请重新输入该学生的C++成绩 : ");
  126.                                                 scanf("%lf", &stu[i].score[2]);
  127.                                         }
  128.                                 }
  129.                         }
  130.                         if (([color=Red]p3 = fopen("stud", "rb")[/color]) == NULL)
  131.                         {
  132.                                 printf("文件无法打开!\n");
  133.                                 exit(0);
  134.                         }
  135.                         if (fwrite(&stu[i], sizeof(struct Student), 1, p3) != 1)
  136.                         {
  137.                                 printf("输入错误!\n");
  138.                         }
  139.                         i = t;
  140.                         printf("\n");
  141.                         //fclose(p3);
  142.                         break;
  143.                 case 4:
  144.                         if ((p4 = fopen("stud", "r")) == NULL)
  145.                         {
  146.                                 printf("文件无法打开!\n");
  147.                                 exit(0);
  148.                         }
  149.                         t = i;
  150.                         for (i = 0; i<2; i++)
  151.                         {
  152.                                 if (fread(&stu[i], sizeof(struct Student), 1, p4) != -1)
  153.                                 {
  154.                                         printf("学生姓名  : %s \n", stu[i].name);
  155.                                         printf("学生的学号: %d \n", stu[i].num);
  156.                                         printf("高数成绩为: %.1lf \n", stu[i].score[0]);
  157.                                         printf("英语成绩为: %.1lf \n", stu[i].score[1]);
  158.                                         printf("C++成绩为 : %.1lf \n", stu[i].score[2]);
  159.                                         printf("\n");

  160.                                 }
  161.                         }
  162.                         i = t;
  163.                         //fclose(p4);
  164.                         break;
  165.                 default:printf("输入错误\n");

  166.                 }
  167.                 printf("\n");
  168.                 printf("***请选择***\n");
  169.                 printf("1***返回主菜单\n");
  170.                 printf("0***退出程序\n");
  171.                 scanf("%d", &a);
  172.                 while (a != 1 && a != 0)
  173.                 {
  174.                         printf("输入错误,请重新输入:");
  175.                         scanf("%d", &a);
  176.                 }

  177.         } while (a == 1);

  178.         //fclose(p2);
  179.         //fclose(p3);
  180.         //fclose(p4);
  181.         return 0;
  182. }
复制代码


没有仔细看 但发现 p3 在打开前使用了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-6 23:12:45 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
struct Student
{
        char name[20];
        int num;
        double score[3];
}stu[SIZE];
int main()
{
        FILE *p1, *p2, *p3, *p4;
        int i = 0, n, j, t, a, b = 0, c;
        if ((p1 = fopen("stud", "wb")) == NULL)
        {
                printf("文件无法打开!\n");
                exit(0);
        }
        do
        {
                printf("***简单的成绩管理系统***\n");
                printf("\n");
                printf("           【请选择】\n");
                printf("【1】***添加一个学生的的记录\n");
                printf("【2】***查看一个学生的记录\n");
                printf("【3】***修改一个学生的记录\n");
                printf("【4】***浏览所有学生记录\n");
                scanf("%d", &n);
                while (n != 1 && n != 2 && n != 3 && n != 4)
                {
                        printf("输入错误,请重新输入:");
                        scanf("%d", &n);
                }
                switch (n)
                {
                case 1:
                        if ((p1 = fopen("stud", "ab")) == NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        printf("请输入学生姓名:");
                        scanf("%s", stu.name);
                        printf("请输入该学生的学号:");
                        scanf("%d", &stu.num);
                        printf("请输入该学生的高数成绩:");
                        scanf("%lf", &stu.score[0]);
                        while (stu.score[0]<0 || stu.score[0]>100)
                        {
                                printf("高数成绩输入错误,请重新输入:");
                                scanf("%lf", &stu.score[0]);
                        }
                        printf("请输入该学生的英语成绩:");
                        scanf("%lf", &stu.score[1]);
                        while (stu.score[1]<0 || stu.score[1]>100)
                        {
                                printf("英语成绩输入错误,请重新输入:");
                                scanf("%lf", &stu.score[1]);
                        }
                        printf("请输入该学生的C++成绩:");
                        scanf("%lf", &stu.score[2]);
                        while (stu.score[2]<0 || stu.score[2]>100)
                        {
                                printf("C++成绩输入错误,请重新输入:");
                                scanf("%lf", &stu.score[2]);
                        }
                        fseek(p1, i*sizeof(struct Student), 1);
                        if (fwrite(&stu, sizeof(struct Student), 1, p1) != 1)
                        {
                                printf("输入错误!\n");
                        }
                        i++;
                        printf("输入成功\n");
                        fclose(p1);
                        break;
                case 2:
                        if ((p2 = fopen("stud", "rb")) == NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        printf("请输入要查看的学生的学号:");
                        scanf("%d", &j);
                        t = i;
                        while (!feof(p2))
                        {
                                if (fread(&stu, sizeof(struct Student), 1, p2) != -1)
                                {
                                        if (j == stu.num)
                                        {
                                                printf("该学生的姓名    : %s \n", stu.name);
                                                printf("该学生的高数成绩: %.1lf \n", stu.score[0]);
                                                printf("该学生的英语成绩: %.1lf \n", stu.score[1]);
                                                printf("该学生的C++成绩 : %.1lf \n", stu.score[2]);
                                        }
                                        else
                                        {
                                                b++;
                                        }
                                }
                        }
                        if (b == SIZE)
                        {
                                printf("该同学记录不存在!!\n");
                        }
                        i = t;
                        printf("\n");
                        fclose(p2);
                        break;
                case 3:
                        printf("请输入要修改学生的学号:");
                        scanf("%d", &j);
                        t = i;
                        for (i = 0; i<SIZE; i++)
                        {
                                if (fread(&stu, sizeof(struct Student), 1, p3) != -1) //不能把此行高亮吗
                                {
                                        if (stu.num == j)
                                        {
                                                printf("该学生姓名: %s \n", stu.name);
                                                printf("请重新输入该学生的高数成绩: ");
                                                scanf("%lf", &stu.score[0]);
                                                printf("请重新输入该学生的英语成绩: ");
                                                scanf("%lf", &stu.score[1]);
                                                printf("请重新输入该学生的C++成绩 : ");
                                                scanf("%lf", &stu.score[2]);
                                        }
                                }
                        }
                        if ((p3 = fopen("stud", "rb")) == NULL)  //不能把此行高亮吗
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        if (fwrite(&stu, sizeof(struct Student), 1, p3) != 1)
                        {
                                printf("输入错误!\n");
                        }
                        i = t;
                        printf("\n");
                        //fclose(p3);
                        break;
                case 4:
                        if ((p4 = fopen("stud", "r")) == NULL)
                        {
                                printf("文件无法打开!\n");
                                exit(0);
                        }
                        t = i;
                        for (i = 0; i<2; i++)
                        {
                                if (fread(&stu, sizeof(struct Student), 1, p4) != -1)
                                {
                                        printf("学生姓名  : %s \n", stu.name);
                                        printf("学生的学号: %d \n", stu.num);
                                        printf("高数成绩为: %.1lf \n", stu.score[0]);
                                        printf("英语成绩为: %.1lf \n", stu.score[1]);
                                        printf("C++成绩为 : %.1lf \n", stu.score[2]);
                                        printf("\n");

                                }
                        }
                        i = t;
                        //fclose(p4);
                        break;
                default:printf("输入错误\n");

                }
                printf("\n");
                printf("***请选择***\n");
                printf("1***返回主菜单\n");
                printf("0***退出程序\n");
                scanf("%d", &a);
                while (a != 1 && a != 0)
                {
                        printf("输入错误,请重新输入:");
                        scanf("%d", &a);
                }

        } while (a == 1);

        //fclose(p2);
        //fclose(p3);
        //fclose(p4);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-6 23:13:55 | 显示全部楼层
看了可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-1-6 23:14:33 | 显示全部楼层
看来可以 (我讨厌错别字)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-14 10:26:10 | 显示全部楼层
记得设置最佳答案哦~~~~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-13 13:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表