C语言帮看一下程序错在哪。

来源:百度知道 编辑:UC知道 时间:2024/05/16 16:11:53
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu_list","wb"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if((fwrite(&stud[i]),sizeof(struct student_type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
main()
{
int i;
for(i=0;i<SIZE;i++)
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
save();
}
才学文件这块,照书打的例子,不知道错在哪,运行不出来,帮看一下,谢谢了。
我看书上介绍的就是if((fp=("文件名","操作类型"))==NULL)书上就是这样介绍的,而且,我在好多例子也都是这样写的,都好用的啊。

你这个不是运行不出来,你已经从键盘输入信息了,这些数据输出到以"stu_list"命名的磁盘文件中.程序运行时,屏幕上并无输出任何信息,只是将信息送到磁盘文件上.你要再执行一个程序,才能输出.

if((fp=fopen("stu_list","wb"))==NULL)
这条语句应该有点问题把,伏值运算副“=”应该是在“==”后执行的吧