C语言 C 中的 fread/fwrite 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2397707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fread/fwrite string in C
提问by Blackbinary
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.
其中 fp 是文件指针。
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.
现在我关闭文件。但是,如果我然后想从文件中读取以获取此数据,我相信我需要读入结构,读取 nameLength 变量,分配足够的内存供名称放置,然后将名称写入变量。
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?
所以很明显我做错了什么。有人能给我一些帮助吗?
回答by John Knoeller
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.
我看到写入有两个问题,您将 record.nameLength 设置得太小,并且您将错误的指针传递给 fwrite 以获取名称。record.name 已经是一个指针。
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.
您在读取时也有问题,因为您没有将字符串中的终止 \0 写入文件,当您回读时,您需要明确添加该终止符。
char *nameString = malloc(sizeof(char)* (record.nameLength + 1));
fread(nameString,sizeof(char),record.nameLength,fp);
nameString[record.NameLength] = 'fwrite(&record.name,sizeof(char),record.nameLength,fp);
';
回答by zneak
The problem is that you pass the pointer to the char*in your fwrite:
问题是您将指针传递给char*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. Fwriteexpects a pointer to the data to write—in your case, that's the pointer to the chardata, not the pointer to the pointer of the chardata.
这意味着不是写名字,而是写名字的内存地址。Fwrite需要一个指向要写入的数据的指针 - 在您的情况下,这是指向char数据的指针,而不是指向数据指针的指针char。
Pass it record.nameinstead of &record.nameand you should be set:
传递它record.name而不是&record.name你应该设置:

