写作结构的数组在C二进制文件 [英] Writing an array of structs to a binary file in C

查看:144
本文介绍了写作结构的数组在C二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有结构的数组,我想写到一个二进制文件。我有一个为write.c程序和read.c程序。该方案为write.c似乎能正常工作,但是当我运行该程序read.c我得到一个分段错误。我是新的C所以这将是巨大的,如果有人能看在我的code任何明显的错误。我保证这不是太长:)

为write.c:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;结构人
{
    烧焦f_name [256];
    焦炭l_name [256];
    INT年龄;
};INT主(INT ARGC,CHAR *的argv [])
{
    结构*人的人;
    INT PEOPLE_COUNT;    的printf(有多少人你想创造:);
    scanf函数(%i的,&安培; PEOPLE_COUNT);
    人=的malloc(sizeof的(结构人)* PEOPLE_COUNT);    INT N;
    为(N = 0; N< PEOPLE_COUNT; N ++)
    {
        的printf(%的人我的名字:N);
        scanf函数(%S,人[N] .f_name);        的printf(%的人我的姓氏,N);
        scanf函数(%S,人[N] .l_name);        的printf(%的人我的年龄:,N);
        scanf函数(%i的,&安培;人[N]。年龄);
    }    FILE *的数据;
    如果((数据= FOPEN(data.bin,世行))== NULL)
    {
        的printf(打开文件时出错\\ n);
        返回1;
    }    FWRITE(人的sizeof(结构人)* PEOPLE_COUNT,1,数据);
    FCLOSE(数据);    返回0;
}

read.c:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;结构人
{
    烧焦f_name [256];
    焦炭l_name [256];
    INT年龄;
};INT主(INT ARGC,CHAR *的argv [])
{
    FILE *的数据;
    如果((数据= FOPEN(data.bin,RB))== NULL)
    {
        的printf(打开文件时出错\\ n);
        返回1;
    }    结构*人的人;    FREAD(人的sizeof(结构人)* 1 / *刚看了一人* / 1,数据);
    的printf(%S \\ n,让人[0] .f_name);    FCLOSE(数据);    返回0;
}

感谢您的帮助!


解决方案

 结构人*人;

这只是分配一个指针结构,但你没有实际结构内容有任何分配的空间。要么使用的malloc 同样你写的程序,或尝试这样的:

 结构的人的人;
FREAD(安培;人的sizeof(人),1,数据);

I have an array of structs I would like to write to a binary file. I have a write.c program and a read.c program. The write.c program seems to be working properly but when I run the read.c program I get a segmentation fault. I'm new to C so It would be great if someone could look over my code for any obvious errors. I promise it's not too long :)

write.c:

#include <stdlib.h>
#include <stdio.h>

struct Person 
{
    char f_name[256];
    char l_name[256];
    int age;
};

int main(int argc, char* argv[])
{
    struct Person* people;
    int people_count;

    printf("How many people would you like to create: ");
    scanf("%i", &people_count);
    people = malloc(sizeof(struct Person) * people_count);  

    int n;
    for (n = 0; n < people_count; n++)
    {
        printf("Person %i's First Name: ", n);
        scanf("%s", people[n].f_name);

        printf("Person %i's Last Name: ", n);
        scanf("%s", people[n].l_name);

        printf("Person %i's Age: ", n);
        scanf("%i", &people[n].age);
    }

    FILE* data;
    if ( (data = fopen("data.bin", "wb")) == NULL )
    {
        printf("Error opening file\n");
        return 1;   
    }

    fwrite(people, sizeof(struct Person) * people_count, 1, data);
    fclose(data);

    return 0;
}

read.c:

#include <stdlib.h>
#include <stdio.h>

struct Person 
{
    char f_name[256];
    char l_name[256];
    int age;
};

int main(int argc, char* argv[])
{
    FILE* data;
    if ((data = fopen("data.bin", "rb")) == NULL)
    {
        printf("Error opening file\n");
        return 1;
    }

    struct Person* people;

    fread(people, sizeof(struct Person) * 1/* Just read one person */, 1, data);
    printf("%s\n", people[0].f_name);

    fclose(data);

    return 0;
}

Thanks for the help!

解决方案

struct Person* people;

This allocates just a pointer to struct, but you don't have any allocated space for actual struct contents. Either use malloc similarly to your write program, or try something like:

struct Person people;
fread(&people, sizeof(people), 1, data);

这篇关于写作结构的数组在C二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆