免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1670 | 回复: 4
打印 上一主题 下一主题

[C] input_data output_data data_write [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-11 19:07 |只看该作者 |倒序浏览
#include<stdio.h>
#include<string.h>
#define SIZE 10


struct student_type
{
        int num;
        char name[10];
        int age;
        char sex;
        float score[5];
}std1[SIZE],std2[SIZE];

void input_data()
{
        FILE *fp;
        int i;
        char infile[10];
   
    printf("Enter the infilename:\n");
    scanf("%s",infile);
        if ((fp = fopen(infile,"rb")) == NULL)
         {
                printf("cannot open infile\n");
                return;
         }
        for (i = 0; i < SIZE; i++)
         {
           if (fread(&std1[i],sizeof(struct student_type),1,fp) != 1)
           { if(feof(fp))
                    {fclose(fp);return;}
                  printf("file read error\n");
           }
         }
        for (i = 0; i < SIZE; i++)
        printf("%4d %-10s %4d %5s %4d %4d %4d %4d %4d\n",
    std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
    std1[i].score[1],std1[i].score[2],
    std1[i].score[3],std1[i].score[4],std1[i].score[5]);
       
        fclose (fp);
}

void output_data()
{
        int i;

    input_data();
    printf("num name  age sex  computer progrom PE english maths\n");
        for (i = 0; i < SIZE; i++)
        printf("%4d %-10s %4d %5s %4d %4d %4d %4d %4d\n",
    std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
    std1[i].score[1],std1[i].score[2],
    std1[i].score[3],std1[i].score[4],std1[i].score[5]);
         
}

void data_write()
{
         FILE  *out;
         char outfile[10];
         int i;

     input_data();
     printf("Enter the outfile name:\n");
     scanf("%s",outfile);
     out = fopen(outfile,"rb");
     for (i = 0; i < SIZE; i++)
     if (fwrite(&std1[i],sizeof(struct student_type),1,out) != 1)
     printf("file write error\n");
     fclose(out);
}

void main()
{
        char ch;
       
        ch = getchar();
        switch(ch)
        { case '1':input_data();break;
          case '2':output_data();break;
          case '3':data_write();break;
        }
}
此函数是为了实现:
设计一个函数,实现将10个学生的数据信息读入到数组std1[10]中。(input_data( )函数)
设计一个函数,实现将10个学生的数据信息输出到屏幕中。(output_data( )函数)
定义一个函数,功能是将数组std1[10]中的数据写入一个名为std.dat的二进制文件中。(data_write( )函数)
但结果难于实现,小弟是莱鸟!!!
向各位高手请教了!

论坛徽章:
0
2 [报告]
发表于 2008-05-11 23:02 |只看该作者
无人知晓?

论坛徽章:
0
3 [报告]
发表于 2008-05-11 23:52 |只看该作者
编译器好像不经过:
   for (i = 0; i < SIZE; i++)
        printf("%4d %-10s %4d %5s %4d %4d %4d %4d %4d\n",
    std1.num,std1.name,std1.age,std1.sex,
    std1.score[1],std1.score[2],
    std1.score[3],std1.score[4],std1.score[5]);
       ??????????????????????

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2008-05-12 00:00 |只看该作者
对着调试器慢慢调吧

论坛徽章:
0
5 [报告]
发表于 2008-05-12 23:47 |只看该作者

回复 #4 cjaizss 的帖子

就是因为难于解决才发帖了!
不然,我自己早已经解决了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP