简单的c语言小程序,找不出原因为什么输入的数据会存不了?

下面是我在visual c++ 6.0写的小程序。
想问下为什么输入的数据会存不了?
//班级成绩查询(C期末作业)
//1、构建一个班级成绩表。
//2、表中至少20个学生,每个学生有5门课的成绩。
//3、算出每个学生的平均分。
//4、根据平均分用动态链表排序。
#include
#include
#define N 3

struct Student
{
unsigned long int sno;
char name[20];
int score[5];
int aver;
struct Student *next_sno;
struct Student *next_aver;
}stu[N];
void save()
{
FILE *p;
int i;

if( (p = fopen( "stu.dat","wb" ) ) == NULL )
{
    printf("无法打开此文件\n");
    exit(0);
}

for( i=0;i<N;i++ )
{
    if( fwrite(&stu[i],sizeof(struct Student),1,p)!=1 )
        printf("file write error\n");
    fclose(p);
}

}
int main()
{
int i;
printf("请输入同学的数据。\n");
for( i=0;i<N;i++ )
{
printf("请输入第%2d位同学的学号:",i+1);
scanf("%ld",&stu[i].sno);
printf(" 姓名:");
scanf("%s",stu[i].name);
printf(" 5个课程成绩(\",\"隔开):");
scanf("%d,%d,%d,%d,%d",&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].score[3],&stu[i].score[4]);
}
save();
return 0;
}
图片说明

以管理员身份运行程序,windows 8限制了应用程序读写文件系统的权限。

写的指定目录试一下:比如d盘
if ((p = fopen("d:/stu.dat", "wb")) == NULL) {

save函数里的循环这里逻辑有问题

就目前初略的看了下。你错的根本原因是,是你在主函数没有定义结构体,
你只声明了一个结构,进程并没有给它分配内存,当你往结构体存入数据时虚拟内存映射不到物理内存
struct Student
}stu[N]; //这样只是告诉了编译器你需要N这样的结构体,并没分配内存

fwrite自带缓冲区,所以不必使用数组存放多个数据一起写入,可以一个一个写入,

//代码改写如下,可供参考

#include
#include

#define N 3
typedef struct Student
{
unsigned long int sno;
char name[20];
int score[5];
int aver;
// struct Student *next_sno; //就当前代码可以省略
// struct Student *next_aver; //就当前代码可以省略
}STU;
void save( STU * stu, FILE * p )
{

if( 1 != fwrite(stu,sizeof(STU),1,p) )
    printf("file write error\n");

}
int main()
{

FILE *fp = NULL;
int i;

if( NULL == (fp = fopen("stu.dat", "wb" )) )
{
    printf("无法打开此文件\n");
    exit(-1);
}


STU stu = {0};  //定义结构体

printf("请输入同学的数据。\n");

for( i=0;i<N;i++ )
{
        printf("请输入第%2d位同学的学号:",i+1);
        scanf("%ld",&stu.sno);
        printf(" 姓名:    ");
        scanf("%s",stu.name);
        printf(" 5个课程成绩(\",\"隔开):");
        scanf("%d,%d,%d,%d,%d",&stu.score[0],&stu.score[1],&stu.score[2],&stu.score[3],&stu.score[4]);

        save(&stu, fp);
}

fclose(fp);

return 0;

}