C 中的 fread/fwrite 字符串
我有一个包含记录的二进制文件。文件的结构如下:
结构(见下文) 名称字符串 地址字符串
有问题的结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现写入有两个问题,您将 record.nameLength 设置得太小,并且将错误的指针传递给 fwrite 作为名称。 record.name 已经是一个指针。
将此更改
为此
您在读取时也遇到问题,因为您没有将字符串中的终止符 \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
to this
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.
问题是您将指针传递给 fwrite 中的 char*:
这意味着您不是写入名称,而是写入名称的内存地址。
Fwrite
需要一个指向要写入的数据的指针 - 在您的情况下,这是指向char
数据的指针,而不是指向char
的指针代码>数据。传递它
record.name
而不是&record.name
并且您应该设置:The problem is that you pass the pointer to the
char*
in your fwrite: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 thechar
data, not the pointer to the pointer of thechar
data.Pass it
record.name
instead of&record.name
and you should be set: