C语言 如何使用 fwrite 将结构写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4155791/
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
How to write a struct to a file using fwrite?
提问by molleman
I'm very new to C, and I am having trouble with fwrite.
我对 C 很陌生,而且我在使用 fwrite 时遇到了麻烦。
I'm looking to use a struct that holds two values:
我希望使用一个包含两个值的结构:
struct keyEncode{
unsigned short key[2];
unsigned short encoded[2];
};
I then declare my struct and a pointer to that struct in my main:
然后我在主文件中声明我的结构和指向该结构的指针:
struct keyEncode keynEncode;
struct keyEncode *storedVal = &keynEncode;
I then assign values to the struct and want to write the struct to a file using fwrite:
然后我将值分配给结构并希望使用 fwrite 将结构写入文件:
keynEncode.key[0] = k1[0];
keynEncode.key[1] = k1[1];
keynEncode.encoded[0] = p[0];
keynEncode.encoded[1] = p[1];
// i tried to use storedVal.key[0] = k1[0]; but i was getting compile errors
fwrite(storedVal, sizeof(storedVal), 0xffff, fp);
Now my problem is that fwrite writes nothing to the file.
现在我的问题是 fwrite 没有向文件写入任何内容。
Where am I going wrong?
我哪里错了?
回答by Hyman
You are using sizeofon a pointer, this won't calculate the size of the effective struct but the one of the pointer (that could be 4 or 8 bytes). Try with sizeof(struct keyEncode)(sizeof(keyEncode)is enough if you are using C++).
您正在使用sizeof指针,这不会计算有效结构的大小,而是计算指针之一(可能是 4 或 8 个字节)。如果您使用的是 C++,请尝试使用sizeof(struct keyEncode)(sizeof(keyEncode)就足够了)。
Then I don't get why you have 0xFFFFas count, shouldn't it be just 1?
那我不明白为什么你有0xFFFF作为计数,不应该只是1吗?
回答by Paul R
Assuming you only have one such struct, then you need to change:
假设您只有一个这样的结构,那么您需要更改:
fwrite(storedVal, sizeof(storedVal), 0xffff, fp);
fwrite(storedVal, sizeof(storedVal), 0xffff, fp);
to
到
fwrite(storedVal, sizeof(*storedVal), 1, fp);
fwrite(storedVal, sizeof(*storedVal), 1, fp);
回答by Jonathan Leffler
The arguments to fwrite()are the data to be printed, the size of one data item, the number of data items, and the file pointer.
的参数fwrite()是要打印的数据、一个数据项的大小、数据项的数量和文件指针。
You have two problems with the sizes:
你的尺寸有两个问题:
- You specify '
sizeof(storedVal)', which is the size of a pointer - not the size of the structure. - You specify that 65,535 of them need to be written.
- 您指定“
sizeof(storedVal)”,它是指针的大小 - 而不是结构的大小。 - 您指定需要写入其中的 65,535 个。
So, you need to use:
所以,你需要使用:
if (fwrite(storedVal, sizeof(*storedVal), 1, fp) != 1)
...error handling...
Note that fwrite()returns the number of items written. In the general case, you have nitems to write, and you should check:
请注意,fwrite()返回写入的项目数。在一般情况下,您有n要写的项目,您应该检查:
if (fwrite(storedVal, sizeof(*storedVal), n, fp) != n)
...error handling...
回答by badgerr
Look at your variable definitions, and look at which one you are using in the sizeof call. Is it correct to be specifying the size of a pointer rather than the size of the data? Also check your usage of fwrite, I think you are specifying the "count" parameter incorrectly. It is defined as
查看您的变量定义,并查看您在 sizeof 调用中使用的变量定义。指定指针的大小而不是数据的大小是否正确?还要检查您对 fwrite 的使用,我认为您错误地指定了“计数”参数。它被定义为
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
The count parameter specifies the numberof blocks of size sizeto write to the file.
count 参数指定要写入文件的大小为size的块数。
I don't really want to give you the answer right away (someone else probably will), as this is tagged as homework - you should be learning :)
我真的不想马上给你答案(其他人可能会),因为这被标记为家庭作业——你应该学习:)
回答by Vladimir Ivanov
FILE *fp = fopen("c:\test", "wb");
if (fp != NULL) {
fwrite(storedVal, sizeof(keynEncode), 1, fp);
fclose(fp);
}
回答by The Archetypal Paul
// i tried to use storedVal.key[0] = k1[0]; but i was getting compile errors
For this one. storedVal is a pointer to the struct. C's pointer dereference operator is -> so you want
对于这个。storageVal 是指向结构的指针。C 的指针解引用运算符是 -> 所以你想要
storedVal->key[0] = k[0];

