鱼C论坛

 找回密码
 立即注册
查看: 2717|回复: 5

看文件操作时做的一个学生成绩,出问题了,各位帮忙看看啊

[复制链接]
发表于 2019-1-1 11:27:07 | 显示全部楼层 |阅读模式

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

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

x
/*建立一个学生成绩文件*/
#include<stdio.h>
#include<stdlib.h>
#define SIZE 1
struct studentdata
{
        int num;
        char name[10];
        int score;
}stud[SIZE];

void save()
{
        FILE *fp;
        int i;
        if((fp=fopen("student.dat","wb"))==NULL)
        {
                printf("cannot open the file!\n");
                exit(0);
        }
        for(i=0;i<SIZE;i++)
        {
                scanf("%d%s%d",&stud[i].num,stud[i].name,&stud[i].score);
                if(fwrite(&stud[i],sizeof(struct studentdata),1,fp)!=1)
                {
                        printf("error write!\n");
                        exit(0);
                }       
        }
        fclose(fp);
}

void display()
{
        FILE *fp;
        int i;
        if((fp=fopen("student.dat","rb"))==NULL)
        {
                printf("cannot open the file!\n");
                exit(0);
        }
        for(i=0;i<SIZE;i++)
        {
                fread(&stud[i],sizeof(struct studentdata),1,fp);
                printf("%d %s %d\n",stud[i].num,stud[i].name,&stud[i].score);
        }
        fclose(fp);
}
int main()
{
        FILE *fp;
       
        printf("输入学生学号,姓名,成绩:\n");
        save();
        display();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-1-1 11:27:19 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-1 13:56:35 | 显示全部楼层
出什么问题?
stud[i].num,stud[i].name,&stud[i].score

还有什么问题?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-1-1 15:24:16 | 显示全部楼层
void display()
{
        FILE *fp;
        int i;
        if((fp=fopen("student.dat","rb"))==NULL)
        {
                printf("cannot open the file!\n");
                exit(0);
        }
        for(i=0;i<SIZE;i++)
        {
                fread(&stud[i],sizeof(struct studentdata),1,fp);
                printf("%d %s %d\n",stud[i].num,stud[i].name,&stud[i].score);
        }
        fclose(fp);
}

printf 最后一个参数前面有个&,你是想输出地址?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-13 21:17:56 | 显示全部楼层
ba21 发表于 2019-1-1 13:56
出什么问题?
stud.num,stud.name,&stud.score

谢谢了啊都忘看论坛了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-13 21:18:27 | 显示全部楼层
rencaixiaomeng 发表于 2019-1-1 15:24
void display()
{
        FILE *fp;

多谢了啊,忘看论坛了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 08:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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