| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1326 人关注过本帖
标题:读取二进制文件时出问题了
只看楼主 加入收藏
lxk1732942
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:450
专家分:425
注 册:2018-9-4
结帖率:96.43%
收藏
 问题点数:0 回复次数:4 
读取二进制文件时出问题了
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
    char name[10];
    char num[12];
    int age;
    struct student *next;
};

int main(void)
{
    struct student *creat(void);
    void myprint(const struct student *);
    void save(FILE *, const struct student *);
    struct student *read_bin(FILE *);

    FILE *fp;
    struct student *head_1, *head_2;

    head_1 = creat();
    myprint(head_1);

    fp = fopen("file", "wb");
    save(fp, head_1);
    fclose(fp);

    fp = fopen("file", "rb");
    head_2 = read_bin(fp);
    myprint(head_2);
    fclose(fp);

    system("pause");

    return 0;
}

//建立单向链表
struct student *creat(void)
{
    struct student *head, *p1, *p2;
    int flag;

    head = NULL;
    do
    {
        p2 = (struct student *)malloc(sizeof(struct student));

        printf("input student's name,number and age:\n");
        scanf("%s%s%d", p2->name, p2->num, &p2->age);

        if (head == NULL)
            head = p2;
        else
            p1->next = p2;
        p1 = p2;

        printf("\ninput 1 to keep on or input 0 to end:");
        scanf("%d", &flag);
    } while (flag);
    p1->next = NULL;

    return head;
}

//输出单向链表
void myprint(const struct student *head)
{
    struct student *p = (struct student *)head;

    while (p)
    {
        printf("%-10s\t%12s\t%2d\n", p->name, p->num, p->age);
        p = p->next;
    }
}

//保存链表至二进制文件
void save(FILE *fp, const struct student *head)
{
    struct student *p = (struct student *)head;
    
    while (p)
    {
        fwrite(p, sizeof(p->next) + sizeof(p->num) + sizeof(p->age), 1u, fp);
        p = p->next;
    }
}

//创建链表读取二进制文件中的信息
struct student *read_bin(FILE *fp)
{
    struct student *head, *p1, *p2;

    head = NULL;
    while (!feof(fp))
    {
        p2 = (struct student *)malloc(sizeof(struct student));

        fread(p2, sizeof(p2->name) + sizeof(p2->num) + sizeof(p2->age), 1u, fp);

        if (head == NULL)
            head = p2;
        else
            p1->next = p2;
        p1 = p2;
    }
    p1->next = NULL;

    return head;
}

运行过程:
图片附件: 游客没有浏览图片的权限,请 登录注册

我输入了两行信息,却读出四行...

[此贴子已经被作者于2019-1-12 20:07编辑过]

搜索更多相关主题的帖子: struct student next head sizeof 
2019-01-12 20:05
lxk1732942
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:450
专家分:425
注 册:2018-9-4
收藏
得分:0 
哦,对了,一共四行信息,前两行是验证链表的建立与输出是否有问题,很显然没有问题,后两行信息才是从文件中读到的,很显然是有问题的,但是不能确定是写入时的问题还是读取时的问题
2019-01-12 20:18
lxk1732942
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:450
专家分:425
注 册:2018-9-4
收藏
得分:0 
自己顶一下
2019-01-12 22:19
lxk1732942
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:450
专家分:425
注 册:2018-9-4
收藏
得分:0 
代码经过修改已经可以正常工作了
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
    char name[10];
    char num[12];
    int age;
    struct student *next;
};

int main(void)
{
    struct student *creat(void);
    void myprint(const struct student *);
    void save(FILE *, const struct student *);
    struct student *read_bin(FILE *);

    FILE *fp;
    struct student *head_1, *head_2;

    head_1 = creat();
    myprint(head_1);

    printf("\n\n\n");

    fp = fopen("file", "wb");
    save(fp, head_1);
    fclose(fp);

    fp = fopen("file", "rb");
    head_2 = read_bin(fp);
    myprint(head_2);
    fclose(fp);

    system("pause");

    return 0;
}

//建立单向链表
struct student *creat(void)
{
    struct student *head, *p1, *p2;
    int flag;

    head = NULL;
    do
    {
        p2 = (struct student *)malloc(sizeof(struct student));

        printf("input student's name,number and age:\n");
        scanf("%s%s%d", p2->name, p2->num, &p2->age);

        if (head == NULL)
            head = p2;
        else
            p1->next = p2;
        p1 = p2;

        printf("\ninput 1 to keep on or input 0 to end:");
        scanf("%d", &flag);
    } while (flag);
    p1->next = NULL;

    return head;
}

//输出单向链表
void myprint(const struct student *head)
{
    struct student *p = (struct student *)head;

    while (p)
    {
        printf("%-10s\t%12s\t%2d\n", p->name, p->num, p->age);
        p = p->next;
    }
}

//链表信息写入二进制文件
void save(FILE *fp, const struct student *head)
{
    struct student *p = (struct student *)head;
    
    while (p)
    {
        fwrite(p, sizeof(struct student) - sizeof(struct student *) , 1u, fp);
        p = p->next;
    }
}

//二进制文件信息读取至链表
struct student *read_bin(FILE *fp)
{
    struct student *head, *p1, *p2;

    head = NULL;
    while (1)
    {
        p2 = (struct student *)malloc(sizeof(struct student));

        if (fread(p2, sizeof(struct student) - sizeof(struct student *), 1u, fp) != 1)
        {
            free(p2);
            break;
        }

        if (head == NULL)
            head = p2;
        else
            p1->next = p2;
        p1 = p2;
    }
    p1->next = NULL;

    return head;
}

图片附件: 游客没有浏览图片的权限,请 登录注册

2019-01-13 11:31
lxk1732942
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:450
专家分:425
注 册:2018-9-4
收藏
得分:0 
代码经过修改已经可以正常工作了
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
    char name[10];
    char num[12];
    int age;
    struct student *next;
};

int main(void)
{
    struct student *creat(void);
    void myprint(const struct student *);
    void save(FILE *, const struct student *);
    struct student *read_bin(FILE *);

    FILE *fp;
    struct student *head_1, *head_2;

    head_1 = creat();
    myprint(head_1);

    printf("\n\n\n");

    fp = fopen("file", "wb");
    save(fp, head_1);
    fclose(fp);

    fp = fopen("file", "rb");
    head_2 = read_bin(fp);
    myprint(head_2);
    fclose(fp);

    system("pause");

    return 0;
}

//建立单向链表
struct student *creat(void)
{
    struct student *head, *p1, *p2;
    int flag;

    head = NULL;
    do
    {
        p2 = (struct student *)malloc(sizeof(struct student));

        printf("input student's name,number and age:\n");
        scanf("%s%s%d", p2->name, p2->num, &p2->age);

        if (head == NULL)
            head = p2;
        else
            p1->next = p2;
        p1 = p2;

        printf("\ninput 1 to keep on or input 0 to end:");
        scanf("%d", &flag);
    } while (flag);
    p1->next = NULL;

    return head;
}

//输出单向链表
void myprint(const struct student *head)
{
    struct student *p = (struct student *)head;

    while (p)
    {
        printf("%-10s\t%12s\t%2d\n", p->name, p->num, p->age);
        p = p->next;
    }
}

//链表信息写入二进制文件
void save(FILE *fp, const struct student *head)
{
    struct student *p = (struct student *)head;
    
    while (p)
    {
        fwrite(p, sizeof(struct student) - sizeof(struct student *) , 1u, fp);
        p = p->next;
    }
}

//二进制文件信息读取至链表
struct student *read_bin(FILE *fp)
{
    struct student *head, *p1, *p2;

    head = NULL;
    while (1)
    {
        p2 = (struct student *)malloc(sizeof(struct student));

        if (fread(p2, sizeof(struct student) - sizeof(struct student *), 1u, fp) != 1)
        {
            free(p2);
            break;
        }

        if (head == NULL)
            head = p2;
        else
            p1->next = p2;
        p1 = p2;
    }
    p1->next = NULL;

    return head;
}

图片附件: 游客没有浏览图片的权限,请 登录注册

2019-01-13 11:31
快速回复:读取二进制文件时出问题了
数据加载中...
 
   



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

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