468,491 Members | 2,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

fread to a struct

-- foo.c --
#include <stdio.h>

int main(void) {

struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --

Is there any problem with the above snippet?
In particular, with reading a struct with fread like that, instead of
reading each member seperately.
Jan 2 '08 #1
8 11408

<vi******@gmail.comwrote in message
news:f1**********************************@c4g2000h sg.googlegroups.com...
-- foo.c --
#include <stdio.h>

int main(void) {

struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --

Is there any problem with the above snippet?
In particular, with reading a struct with fread like that, instead of
reading each member seperately.
What you expect may not happen. I guess stdin is opened for reading in text
mode , not binary mode.
fread and fwrite reads and writes in binary (raw bytes).

Jan 2 '08 #2
vi******@gmail.com said:
-- foo.c --
#include <stdio.h>

int main(void) {

struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --

Is there any problem with the above snippet?
Add:

printf("[%d] [%d]\n", foo.a, foo.b);

after the fread, recompile, and test. Whilst this cannot prove that there
is not a problem with the snippet, it may be able to prove that there /is/
a problem.

I doubt very much whether you will get the results you desire!
In particular, with reading a struct with fread like that, instead of
reading each member seperately.
If it was written like that, it can be read like that on the same platform
(same compiler, same switch settings, yadayadayada). So you can do this:

#include <stdio.h>
#define TESTFILE "test.foo"
int main(void)
{
struct { int a; int b; } foo = { 6, 42 };
FILE *fpw = fopen(TESTFILE, "wb");
if(fpw != NULL)
{
int ok = 0;
printf("before write: foo.a = %d\nfoo.b = %d\n", foo.a, foo.b);
fwrite(&foo, 1, sizeof foo, fpw);
ok = !ferror(fpw);
fclose(fpw);
if(ok)
{
FILE *fpr = fopen(TESTFILE, "rb");
if(fpr != NULL)
{
foo.a = 10; foo.b = 20; /* any old junk, just to prove the point */
printf("after reset: foo.a = %d\nfoo.b = %d\n", foo.a, foo.b);
fread(&foo, 1, sizeof foo, fpr);
if(!ferror(fpr))
{
printf("after read: foo.a = %d\nfoo.b = %d\n", foo.a, foo.b);
}
fclose(fpr);
}
}
}
return 0;
}

This code has been (rather cursorily) tested, and a bug removed. One kudos
point available to whoever can work out what the bug was.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 2 '08 #3
On Jan 2, 1:51 pm, Richard Heathfield <r...@see.sig.invalidwrote:
This code has been (rather cursorily) tested, and a bug removed. One kudos
point available to whoever can work out what the bug was.
I'd assume that you pass nmemb, size to fread/fwrite and not size,
nmemb.

Thanks for the replies, I know what to do now.
Jan 2 '08 #4
vi******@gmail.com said:
On Jan 2, 1:51 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>This code has been (rather cursorily) tested, and a bug removed. One
kudos point available to whoever can work out what the bug was.
I'd assume that you pass nmemb, size to fread/fwrite and not size,
nmemb.
In fact, I did get them the wrong way round, not that it matters
particularly, so well spotted, but no, that wasn't the bug (which, I
stress, was *fixed* in the posted code).
Thanks for the replies, I know what to do now.
Excellent.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 2 '08 #5
"Ravishankar S" <ra***********@in.bosch.comwrites:
<vi******@gmail.comwrote in message
news:f1**********************************@c4g2000h sg.googlegroups.com...
>-- foo.c --
#include <stdio.h>

int main(void) {

struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --

Is there any problem with the above snippet?
In particular, with reading a struct with fread like that, instead of
reading each member seperately.

What you expect may not happen. I guess stdin is opened for reading in text
mode , not binary mode.
fread and fwrite reads and writes in binary (raw bytes).
No, fread reads (as if) by repeated calls to fgetc. If the stream is
opened in text mode, it will read in text mode; if it's opened in
binary mode, it will read in binary mode. (Likewise for fwrite
repeatedly calling fputc.)

Because of this, reading in text mode using fread rarely makes sense;
it's *intended* to be used in binary mode.

In the above code, the fread() call will attempt to read sizeof(foo)
bytes in text mode from stdin, and store them in foo. If you're
expecting to type "10 20" and have the values 10 and 20 stored in
foo.a and foo.b, you'll be disappointed.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 2 '08 #6
vi******@gmail.com wrote:
# -- foo.c --
# #include <stdio.h>
#
# int main(void) {
#
# struct { int a; int b; } foo;
# fread(&foo, sizeof foo, 1, stdin);
# return 0;
# }
# -- foo.c --
#
# Is there any problem with the above snippet?

ANSI C does not guarentee sizeof struct foo will be the same
for different compilers or different versions of the same
compilers or different compilations of the same compilers.
Unless you can get a guarentee from the compilers you're
interested in, it's safer to read and write individual int
fields yourself.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
Jan 2 '08 #7
vi******@gmail.com wrote:
-- foo.c --
#include <stdio.h>

int main(void) {

struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --

Is there any problem with the above snippet?
In particular, with reading a struct with fread like that, instead of
reading each member seperately.
First problem is the *padding* issue in struct. Hence, different
versions of your compiler may break this code, or different compilers
may use different alignment of struct members, and finally just changing
compiler setting differently... may break it.

The second problem is endianness. If 'foo' is orginating from a
hetrogenious system, the code may brake.

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Jan 3 '08 #8
Tor Rustad <se*****@dev.null.invalidwrites:
vi******@gmail.com wrote:
>-- foo.c --
#include <stdio.h>

int main(void) {

struct { int a; int b; } foo;
fread(&foo, sizeof foo, 1, stdin);
return 0;
}
-- foo.c --

Is there any problem with the above snippet?
In particular, with reading a struct with fread like that, instead of
reading each member seperately.

First problem is the *padding* issue in struct. Hence, different
versions of your compiler may break this code, or different compilers
may use different alignment of struct members, and finally just
changing compiler setting differently... may break it.

The second problem is endianness. If 'foo' is orginating from a
hetrogenious system, the code may brake.
And the 3rd through Nth problems are all the other possible variations
in representation. For the structure shown, the possible variations
include:
Different number of bytes in an int.
Different number of bits in a byte.
Different padding bits within an int.
Different representations for negative int values.

The last three are relatively unlikely.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 4 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Luc Holland | last post: by
4 posts views Thread by janssenssimon | last post: by
3 posts views Thread by Giox | last post: by
8 posts views Thread by M. ┼hman | last post: by
6 posts views Thread by Claude Yih | last post: by
5 posts views Thread by loudking | last post: by
20 posts views Thread by xiao | last post: by
15 posts views Thread by =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.