| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2025 人关注过本帖
标题:怎么用fwrite将double类型数据写入文件中fread读取文件中的double类型数据
只看楼主 加入收藏
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
怎么用fwrite将double类型数据写入文件中fread读取文件中的double类型数据
ypedef struct information
{
    char name[32];
    char ID[20];//身份证号码
    char account[32];
    char code[7];//密码
    double money;//8个字节
    struct information *previous , *next;//4个字节
}information , *Information ,a;
我用fwrite 将这个结构体写入文件:fwrite (&a , 99 , 1 , fp);然后用fread (&a , 99 , 1 , fp);读取数据,输出时其它的成员的数据都正确,但是a.money得到的总是一个垃圾值。为什么会这样啊。
搜索更多相关主题的帖子: fwrite double 类型 数据 文件 
2017-09-23 09:20
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10559
专家分:42996
注 册:2014-5-20
收藏
得分:10 
#include <stdio.h>

typedef struct _information
{
    char name[32];
    char ID[20];//身份证号码
    char account[32];
    char code[7];//密码
    double money;//8个字节
    struct information *previous , *next;//4个字节
}information , *Information , a;

main()
{
   
    information am = {"ABCDEFG","101202303404505606","abcdefg","abc123",123456.789,NULL,NULL};
    FILE *fp = fopen("test.dat", "wb");
    fwrite(&am, sizeof(information), 1 , fp);
    fclose(fp);
    information am2;
    fp = fopen("test.dat", "rb");
    fread(&am2, sizeof(information), 1 , fp);
    fclose(fp);
    printf("   name: %s\n", am2.name);
    printf("     ID: %s\n", am2.ID);
    printf("account: %s\n", am2.account);
    printf("   code: %s\n", am2.code);
    printf("  money: %lf\n", am2.money);
}
2017-09-23 10:58
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10559
专家分:42996
注 册:2014-5-20
收藏
得分:10 
以下是引用yu965634383在2017-9-23 09:20:44的发言:

ypedef struct information
{
    char name[32];
    char ID[20];//身份证号码
    char account[32];
    char code[7];//密码
    double money;//8个字节
    struct information *previous , *next;//4个字节
}information , *Information ,a;
我用fwrite 将这个结构体写入文件:fwrite (&a , 99 , 1 , fp);然后用fread (&a , 99 , 1 , fp);读取数据,输出时其它的成员的数据都正确,但是a.money得到的总是一个垃圾值。为什么会这样啊。

内存分配有个叫“字节对齐”问题,通常是用4字节对齐,这时可能要取104个字节才能完整取出这几个数据。
可以这样比较看看:
#include <stdio.h>
main()
{
    struct data
    {
        char name[32];
        char ID[20];//身份证号码
        char account[32];
        char code[7];//密码
        double money;//8个字节
    };
    printf("%d\n", sizeof(struct data));
}
或者:
#include <stdio.h>
#pragma pack (1)
main()
{
    struct data
    {
        char name[32];
        char ID[20];//身份证号码
        char account[32];
        char code[7];//密码
        double money;//8个字节
    };
    printf("%d\n", sizeof(struct data));
}

这样会更好表达些:
#include <stdio.h>

typedef struct data
{
    char name[32];
    char ID[20];//身份证号码
    char account[32];
    char code[7];//密码
    double money;//8个字节
}DATA, *PDATA;

typedef struct information
{
    DATA d;
    struct information *previous , *next;//4个字节
}information, *Information;

int main(void)
{
    information a = {"ABCDEFG","101202303404505606","abcdefg","abc123",123456.789,NULL,NULL};
    FILE *fp = fopen("test.dat", "wb");
    fwrite(&a, sizeof(DATA), 1 , fp);
    fclose(fp);
    DATA b;
    fp = fopen("test.dat", "rb");
    fread(&b, sizeof(DATA), 1 , fp);
    fclose(fp);
    printf("   name: %s\n", b.name);
    printf("     ID: %s\n", b.ID);
    printf("account: %s\n", b.account);
    printf("   code: %s\n", b.code);
    printf("  money: %lf\n", b.money);
    return 0;
}


[此贴子已经被作者于2017-9-23 16:05编辑过]

2017-09-23 15:59
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
谢谢啦,终于搞定了。原来内存分配还有这样的要求,以前都不知道,以为把结构体所有成员所占字节空间加起来就可以了。怪不得明明算对了,但有时候能成功读取数据,有时会出现乱码。

[此贴子已经被作者于2017-9-23 18:01编辑过]


菜鸟一枚,求各位大神多多关照。
2017-09-23 17:52
快速回复:怎么用fwrite将double类型数据写入文件中fread读取文件中的double类型 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016492 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved