ÇFREAD()读取奇迹般地动态分配结构成员,怎么样? [英] C fread() magically reading dynamically allocated struct members, how?

查看:149
本文介绍了ÇFREAD()读取奇迹般地动态分配结构成员,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对我的工作在更大的项目编写一个测试程序。它与写作结构数据做磁盘的fwrite(),然后读取数据传回用fread()。该结构的一个成员是动态分配的。

This is a test program that I have written for a larger project that I am working on. It has to do with writing struct data to disk with fwrite() and then reading that data back with fread(). One member of the struct is dynamically allocated.

首先,这里是我的code

First, here is my code

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

#define STRING_LEN  128

struct Person {
    int age;
    char *name;
};

int main(int argc, const char *argv[])
{
    struct Person *person = calloc(1, sizeof(struct Person));
    person->age = 22;
    person->name = calloc(STRING_LEN, sizeof(char));

    char *name = "Name that is really, really, really, really, really, really, long.";
    strncpy(person->name, name, STRING_LEN);

    FILE *out_file = fopen("rw.out", "w");
    fwrite(person, sizeof(struct Person), 1, out_file);
    fclose(out_file);

    FILE *in_file = fopen("rw.out", "r");
    struct Person *person_read = calloc(1, sizeof(struct Person));
    fread(person_read, sizeof(struct Person), 1, in_file);
    fclose(in_file);

    printf("%d %s\n", person_read->age, person_read->name);

    free(person->name);
    free(person);
    free(person_read);

    return 0;
}

而outpout

And the outpout

22 Name that is really, really, really, really, really, really, long.

我的问题是,为什么是这方面的工作?不应该fwrite()将只写地址'名'包含(即字符串的开头的地址)?也就是说,我在的sizeof(结构人)传递给fwrite()将,但它是写在名称所指向的字符串。

My question is, why is this working? Shouldn't fwrite() only write the address that 'name' contains (i.e., the address of the beginning of the string)? That is, I am passing in sizeof(struct Person) to fwrite() and yet it is writing the string the 'name' is pointing to.

更混乱对我来说是FREAD的行为()。再次,如果我传递的sizeof(结构人),这是怎么'名'的实际值读?而如何为它分配内存?

Even more confusing to me is the behavior of fread(). Again, if I am passing sizeof(struct Person), how is the actual value of 'name' being read? How is the memory for it being allocated?

我的$ P $如何使用FWRITE()+ FREAD()pvious的理解是,我将不得不为手动编写的名称被指向手动读取数据的数据,然后再复制对于结构和名称成员都分配内存后的字符串。换句话说,我将不得不遍历任何指针自己,写入数据,然后读取数据回以相同的顺序。

My previous understanding of how to use fwrite() + fread() was that I would have to "manually" write the data that 'name' was pointing to, "manually" read that data, and then copy that string after allocating memory for both the structure and the 'name' member. In other words, I would have to traverse any pointers myself, write the data, and then read that data back in the same order.

修改:丹和其他人是正确的。我已经看过用XXD输出文件:

EDIT: Dan and the others are correct. I have looked at the output file with xxd:

0000000: 1600 0000 0000 0000 30a0 d900 0000 0000  ........0.......

如果我打印出'名'写之前和阅读是相同的(0xd9a030),其输出从XXD匹配之后包含地址。

If I print out the address that 'name' contains before writing and after reading it is the same (0xd9a030), which matches the output from xxd.

推荐答案

您正在编写的结构,这是一个int其次是一个指向字符串的数据。这只是数据,如别的,你知道它有多长,因为结构是固定长度 - 一个int加的指针。你读同一指针,以相同的名称字符串作为原。这个名字本身既不是写也不会读。

You are writing the data in the struct, which is an int followed by a pointer to a string. It's just data like anything else, and you know how long it is because the struct is fixed length - an int plus a pointer. You read the same pointer to the same name string as the original. The name itself is neither written nor read.

这篇关于ÇFREAD()读取奇迹般地动态分配结构成员,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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