注册 登录
编程论坛 C语言论坛

fwrite写入数据地址对不上

domore 发布于 2019-07-30 04:22, 2100 次点击
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
#include<stdio.h>
#include<stdio.h>        
#define SIZE 10
struct Student_type        //定义全局结构体变组stud,包含10个学生数据
    {
    char name[10];
    int num;
    int age;
    char addr[15];
    }stud[SIZE];
FILE *fp;
void save()    //定义save函数,向文件输出SIZE个学生的数据
    {
    //FILE *fp;原来是在这一行的,为了在main函数中也能输出fp,我把它声明为了全局变量
    int i;
    if((fp=fopen("stu.dat","wb"))==NULL)
        {
        printf("can not open file\n");
        return ;
        }
    printf("before:\nfp addr:%d, stud addr:%d,\nsize of a member:%d\n",fp,stud,sizeof(struct Student_type));    //我自己加的
    for(i=0;i<SIZE;i++)
        if(fwrite(&stud[i],sizeof(struct Student_type),1,fp)!=1)
            printf("file write error");
            return ;
    fclose(fp);
    }
int main()
    {
    int i;
    printf("please enter data of students:\n");
    for(i=0;i<SIZE;i++)
        scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
    save();
    printf("now:\nfp addr:%d, stud addr:%d,\nsize of a member:%d\n",fp,stud,sizeof(struct Student_type));        //我自己加的
    return 0;
    }

[此贴子已经被作者于2019-7-30 06:10编辑过]

4 回复
#2
吹水佬2019-07-30 05:12
“对不上”是什么意思?具体说说
另:最好能提供代码测试
#3
domore2019-07-30 05:58
对不上是指地址不等,我输出了fp和stud的地址,是不等的,而实际上应该是相等的
#include<stdio.h>
#define SIZE 10
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.dat","wb"))==NULL)
        {
        printf("can not 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");
            return ;
    fclose(fp);
    }
int main()
    {
    int i;
    printf("please enter data of students:\n");
    for(i=0;i<SIZE;i++)
        scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
    save();
    return 0;
    }
#4
domore2019-07-30 06:12
用我贴子里的代码  
我重新上传了  
#5
H_M2019-08-09 23:49
回复 3楼 domore
没有问题呀,我修改了一下代码,进行了调试。
修改代码为(红色处为修改):

#include<stdio.h>
#include<stdio.h>        
#define SIZE 1
struct Student_type        //定义全局结构体变组stud,包含10个学生数据
    {
    char name[10];
    int num;
    int age;
    char addr[15];
    }stud[SIZE];
FILE *fp;
void save()    //定义save函数,向文件输出SIZE个学生的数据
    {
    //FILE *fp;原来是在这一行的,为了在main函数中也能输出fp,我把它声明为了全局变量
    int i;
    if((fp=fopen("G:/text/stu.dat","wb"))==NULL)
        {
        printf("can not open file\n");
        return ;
        }
    printf("before:\nfp addr:%d, stud addr:%d,\nsize of a member:%d\n",fp,stud[1],sizeof(struct Student_type));    //我自己加的
    for(i=0;i<SIZE;i++)
        if(fwrite(&stud[i],sizeof(struct Student_type),1,fp)!=1)
            printf("file write error");
            return ;
    fclose(fp);
    }
int main()
    {
    int i;
    printf("please enter data of students:\n");
    for(i=0;i<SIZE;i++)
        scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
    save();
    printf("now:\nfp addr:%d, stud addr:%d,\nsize of a member:%d\n",fp,stud,sizeof(struct Student_type));        //我自己加的
    return 0;
    }

只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2019-8-9 23:56编辑过]

1