How to write a struct to a file using fwrite?
Solution 1
You are using sizeof
on 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++).
Then I don't get why you have 0xFFFF
as count, shouldn't it be just 1
?
Solution 2
Assuming you only have one such struct, then you need to change:
fwrite(storedVal, sizeof(storedVal), 0xffff, fp);
to
fwrite(storedVal, sizeof(*storedVal), 1, fp);
Solution 3
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.
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.
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 n
items to write, and you should check:
if (fwrite(storedVal, sizeof(*storedVal), n, fp) != n)
...error handling...
molleman
Avid rugby player, it student who is doing his final year project at the moment and finding it quite tough! gwt hibernate gilead
Updated on January 29, 2020Comments
-
molleman about 4 years
I'm very new to C, and I am having trouble with 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:
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.
Where am I going wrong?
-
Rabeel over 13 years
sizeof(storedVal)
will give the size of the pointer, not the type pointed to. Should besizeof(*storedVal)
-
molleman over 13 yearsok thank you very much, i dont really understand the third parameter in the fwrite, if you could give some insight that would be great???
-
Paul R over 13 years@Worrier, @Paul: I think the above two comments were probably written while I was still editing.
-
Tom Hennen over 13 yearsAlso, in the first two lines, 'file' should be 'fp'.
-
molleman over 13 yearsalso i would like to write the value of the struct to a binary file
-
Paul R over 13 yearsyou haven't installed Telepathy 1.0 ? ;-)
-
Jack over 13 yearsthe third parameter is used in combination with size if you want to write multiple items with just a fwrite. Basically the total number of bytes written should be
(second parameter * third parameter)
. With second you define the size of every item while third decides how many of them you are gonna write, like if you need to write out an array. -
Alexander Aleksandrovič Klimov over 13 yearsThe second param is the size of each of the items you are writing. The third is the number of those you are writing. It returns the number actually written, so you should check the return value to ensure everything happened as expected
-
Jack over 13 yearsYes, what you are gonna write with fwrite is binary if the fields of the struct are binary, ASCII if they are
char *
. Of course they'll be mixed together. -
Nisse Engström about 9 years