C语言,我先写了一个程序将信息写入文件,然后关掉空间,重新写了一个程序读取文件信息!为什么读不了
#include<stdio.h>//写入信息的程序!#include<stdlib.h>#defineN3typedefstructstudent{charname[2...
#include<stdio.h>//写入信息的程序!
#include<stdlib.h>
#define N 3
typedef struct student
{
char name[20];
char address[20];
int age;
int num;
}type;
type s[N];
void save()
{
FILE*fp;
int i;
if((fp=fopen("stu.dat","wb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<N;i++)
if(fwrite(&s[i],sizeof(type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void main()
{
int i;
printf("please enter data of students:\n");
for(i=0;i<N;i++)
scanf("%s%d%d%s",s[i].name,&s[i].age,&s[i].num,s[i].address);
save();
}
读入信息的文件!
#include<stdio.h>
#include<stdlib.h>
#define N 3
typedef struct student
{
char name[20];
int age;
int num;
char address[20];
}type;
type s[N];
void main()
{
FILE*fp;
int i;
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("cannot open\n");
exit(0);
}
for(i=0;i<N;i++)
{
fread(&s[i],sizeof(type),1,fp);
printf("%-10s%4d%4d%-15s\n",s[i].name,s[i].age,s[i].num,s[i].address);
}
fclose(fp);
} 展开
#include<stdlib.h>
#define N 3
typedef struct student
{
char name[20];
char address[20];
int age;
int num;
}type;
type s[N];
void save()
{
FILE*fp;
int i;
if((fp=fopen("stu.dat","wb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<N;i++)
if(fwrite(&s[i],sizeof(type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void main()
{
int i;
printf("please enter data of students:\n");
for(i=0;i<N;i++)
scanf("%s%d%d%s",s[i].name,&s[i].age,&s[i].num,s[i].address);
save();
}
读入信息的文件!
#include<stdio.h>
#include<stdlib.h>
#define N 3
typedef struct student
{
char name[20];
int age;
int num;
char address[20];
}type;
type s[N];
void main()
{
FILE*fp;
int i;
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("cannot open\n");
exit(0);
}
for(i=0;i<N;i++)
{
fread(&s[i],sizeof(type),1,fp);
printf("%-10s%4d%4d%-15s\n",s[i].name,s[i].age,s[i].num,s[i].address);
}
fclose(fp);
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询