C 中的 fread/fwrite 字符串

发布于 2024-08-24 04:23:14 字数 1087 浏览 12 评论 0原文

我有一个包含记录的二进制文件。文件的结构如下:

结构(见下文) 名称字符串 地址字符串

有问题的结构:

typedef struct{
    char * name;
    char * address;
    short addressLength, nameLength;
    int phoneNumber;
}employeeRecord;
employeeRecord record;

我得到的名称是这样的:

char name[50];
printf("\nName:");
fgets(name,50,stdin);
record.nameLength = strlen(name)-1;
record.name = malloc(sizeof(char)*record.nameLength);
strcpy(record.name,name);

我写结构,名称,然后地址(如上所述)。

fwrite(&record.name,sizeof(char),record.nameLength,fp);

其中 fp 是文件指针。

现在我关闭文件。 但是,如果我想从文件中读取数据以取回数据,我相信我需要读取结构,读取 nameLength 变量,为名称分配足够的内存,然后将名称读取到变量中。

像这样:

char *nameString = malloc(sizeof(char)*record.nameLength);
fread(nameString,sizeof(char),record.nameLength,fp);
printf("\nName: %s",nameString);

但是,当我尝试这样做时,我没有得到有效的数据。 示例:

Input name is: Joseph (6 characters)
Output data: 
Name length is 6 (correct), 
Name is  �A        � (aka garbage)

很明显我做错了什么。有人可以给我一些帮助吗?

I have a binary file which contains records. The structure of the file is as such:

Structure (see below)
Name String
Address String

The structure in question:

typedef struct{
    char * name;
    char * address;
    short addressLength, nameLength;
    int phoneNumber;
}employeeRecord;
employeeRecord record;

I get the name as such:

char name[50];
printf("\nName:");
fgets(name,50,stdin);
record.nameLength = strlen(name)-1;
record.name = malloc(sizeof(char)*record.nameLength);
strcpy(record.name,name);

I write the structure, the the name, then the address (as mentioned above).

fwrite(&record.name,sizeof(char),record.nameLength,fp);

where fp is a file pointer.

Now i close the file.
However, if i then want to read from the file to get this data back, I believe I need to read in the structure, read the nameLength variable, malloc enough memory for the name to sit in, then fread the name into the variable.

Like so:

char *nameString = malloc(sizeof(char)*record.nameLength);
fread(nameString,sizeof(char),record.nameLength,fp);
printf("\nName: %s",nameString);

However, when i attempt this, i do not get valid data.
Example:

Input name is: Joseph (6 characters)
Output data: 
Name length is 6 (correct), 
Name is  �A        � (aka garbage)

So obviously im doing something wrong. Could someone give me some help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-08-31 04:23:14

我发现写入有两个问题,您将 record.nameLength 设置得太小,并且将错误的指针传递给 fwrite 作为名称。 record.name 已经是一个指针。

将此更改

record.nameLength = strlen(name)-1;
...
fwrite(&record.name,sizeof(char),record.nameLength,fp);

为此

record.nameLength = strlen(name);
...
fwrite(record.name,sizeof(char),record.nameLength,fp);

您在读取时也遇到问题,因为您没有将字符串中的终止符 \0 写入文件中,当您读回时,您需要显式添加该终止符。

char *nameString = malloc(sizeof(char)* (record.nameLength + 1));
fread(nameString,sizeof(char),record.nameLength,fp);
nameString[record.NameLength] = '\0';

I see two problems with the write, you are setting record.nameLength to be too small, and you are passing the wrong pointer to fwrite for the name. record.name is already a pointer.

Change this

record.nameLength = strlen(name)-1;
...
fwrite(&record.name,sizeof(char),record.nameLength,fp);

to this

record.nameLength = strlen(name);
...
fwrite(record.name,sizeof(char),record.nameLength,fp);

You also have a problem on the read, since you aren't writing the terminating \0 from the strings into your file, when you read back, you need to add that terminator explicitly.

char *nameString = malloc(sizeof(char)* (record.nameLength + 1));
fread(nameString,sizeof(char),record.nameLength,fp);
nameString[record.NameLength] = '\0';
晚雾 2024-08-31 04:23:14

问题是您将指针传递给 fwrite 中的 char*:

fwrite(&record.name,sizeof(char),record.nameLength,fp);

这意味着您不是写入名称,而是写入名称的内存地址。 Fwrite 需要一个指向要写入的数据的指针 - 在您的情况下,这是指向 char 数据的指针,而不是指向 char 的指针代码>数据。

传递它 record.name 而不是 &record.name 并且您应该设置:

fwrite(record.name, sizeof(char), record.nameLength, fp);

The problem is that you pass the pointer to the char* in your fwrite:

fwrite(&record.name,sizeof(char),record.nameLength,fp);

This means that instead of writing the name, you're writing the memory address of the name. Fwrite expects a pointer to the data to write—in your case, that's the pointer to the char data, not the pointer to the pointer of the char data.

Pass it record.name instead of &record.name and you should be set:

fwrite(record.name, sizeof(char), record.nameLength, fp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文