将char指针作为结构成员写入文件问题 [英] Writing char pointer as struct member to file issue

查看:227
本文介绍了将char指针作为结构成员写入文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作为char *的结构成员,并在结构初始化时将其分配给字符串文字"John",如下所示.字符串可以通过printf正常打印.

但是,如果我使用fwrite将字符串写入文件,则会在文件中读回垃圾.

如果我使用char数组而不是char *(如图所示在struct中注释),并将其写入文件,则可以按预期读回文件中的字符串.我听不懂在两种情况下fwrite都不应该使用指针并将正确的字符串写入文件吗?

此外,如果我声明一个单独的char *并将其指向字符串文字并将其写入文件,则可以按预期方式将其读回.在此先非常感谢您对解释的任何帮助. (我在Windows上将代码块IDE与Mingw编译器一起使用).

已更新:包括使用的实际代码

int main()
{
    struct person
    {
        //char name[20];
        char* name;
        int age;
    };

    struct person p1 = {"John", 25};

    printf("%s\n", p1.name);
    FILE* fp = fopen("test.txt", "w");
    fwrite(&p1, 1, 10, fp);
    fclose(fp);
    return 0;
}

解决方案

注释者说,您提供的代码不能提供足够的信息来解决您的问题.看起来错误出在代码的其他地方.我可以像这样将您的代码转换为 MCVE :

#include <stdio.h>

struct person
{
    //char name[20];
    char* name;
    int age;
};

int main(void)
{
    struct person p1 = {"John", 25};

    printf("%s\n", p1.name);

    FILE *fp = fopen("test.txt", "w");
    fwrite(p1.name, 1, 10, fp);
    fclose(fp);

    char buffer[20];
    fp = fopen("test.txt", "r");
    fread(buffer, 1, 10, fp);
    fclose(fp);

    printf("The stored name is: %s\n", buffer);

    return 0;
}

但是我确信您拥有的是不同的,因为此代码有效:

John
The stored name is: John

更新

从您提供的新代码中,我可以看到您的问题是您正在将struct的内容写入文件,而不是像原始代码示例中那样将字符串写入文件.

使用的原始代码:

fwrite(p1.name, 1, 10, fp);

并将字符串p1.name的10个字节写入文件"test.txt"(吹过字符串"John"NUL终止符,并保存了在打印字符串时看不到的垃圾值). /p>

新代码使用:

fwrite(&p1, 1, 10, fp);

struct p1的前10个字节保存到文件"test.txt".

struct包含char name[20];时,前10个字节是存储在字符数组name中的char,这就是您的代码在这种情况下似乎可以工作的原因.

struct包含char *name;时,保存的前几个字节属于指针name(在我的系统中为8个字节),而不是字符串文字"John".接下来的几个字节属于int age(我的系统上为4个字节).在这种情况下,保存的是这些值,而不是字符串文字"John"char.

请注意,指针和int的大小在不同的系统上可能会有所不同,并且完全有可能为指针分配4个字节,为int提供4个字节,剩下的2个字节将被保存.不是struct的一部分.另外,尽管这并不影响您,但struct的第一个成员之后可能会有填充.

I have a struct member as a char * and assign it to a string literal "John" on the struct initialisation as shown below. The string prints fine with printf.

However, if I write this string to a file using fwrite, in the file I read back garbage.

If I use a char array instead of a char * (commented out in the struct as shown), and write it to the file, I can read back the string in the file as expected. I can't understand this. Shouldn't fwrite in both cases take the pointer and write the correct string to the file?

Furthermore , if I declare a separate char * and point it to a string literal and write it to the file, I can read it back as expected. Many thanks in advance for any help with explanation. (I am using codeblocks IDE with Mingw compiler on Windows).

UPDATED : To include actual code used

int main()
{
    struct person
    {
        //char name[20];
        char* name;
        int age;
    };

    struct person p1 = {"John", 25};

    printf("%s\n", p1.name);
    FILE* fp = fopen("test.txt", "w");
    fwrite(&p1, 1, 10, fp);
    fclose(fp);
    return 0;
}

解决方案

The commenters are saying that the code you provided does not give enough information to solve your problem. It looks like the error lies elsewhere in your code. I can convert your code to a MCVE like this:

#include <stdio.h>

struct person
{
    //char name[20];
    char* name;
    int age;
};

int main(void)
{
    struct person p1 = {"John", 25};

    printf("%s\n", p1.name);

    FILE *fp = fopen("test.txt", "w");
    fwrite(p1.name, 1, 10, fp);
    fclose(fp);

    char buffer[20];
    fp = fopen("test.txt", "r");
    fread(buffer, 1, 10, fp);
    fclose(fp);

    printf("The stored name is: %s\n", buffer);

    return 0;
}

But I am sure that what you have differs, because this code works:

John
The stored name is: John

Update

From the new code that you provided, I can see that your problem is that you are writing the contents of a struct to a file instead of writing a string to a file, as in your original code example.

The original code used:

fwrite(p1.name, 1, 10, fp);

and wrote 10 bytes of the string p1.name to the file "test.txt" (blowing right past the NUL terminator of the string "John" and saving garbage values that would not be seen on printing the string).

The new code uses:

fwrite(&p1, 1, 10, fp);

saving the first 10 bytes of the struct p1 to the file "test.txt".

When the struct contains char name[20];, the first 10 bytes are chars stored in the character array name, and this is why your code appeared to work in this case.

When the struct instead contains char *name;, the first few bytes saved belong to the pointer name(8 bytes on my system), not to the string literal "John". The next few bytes belong to the int age(4 bytes on my system). It is these values and not the chars of the string literal "John" that are being saved in this case.

Note that the sizes of pointers and ints may be different on different systems, and it is entirely possible to have 4 bytes for the pointer and 4 bytes for the int, leaving 2 bytes which are saved that are not part of the struct. Also, though this does not affect you here, there may be padding after the first member of the struct.

这篇关于将char指针作为结构成员写入文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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