fread + array

Started by
7 comments, last by Zeusel 22 years, 3 months ago
float blah[3]; fread(&blah, 4, 3, file); does this read 3 floats from a file and store them correctly? can it be used if the floats are allocated with calloc?
"be fair! ... always"Zeusel
Advertisement
No, that wont work. blah is an ''array'', but in C/C++ arrays are just pointers to buffers, so you don''t need to take the reference of blah.

if you want to read 3 floats like that, try:

  float blah[3];fread(blah, sizeof(float), 3, file);  


You can pass a pointer to any buffer to fread().. just make sure to keep track of the proper sizes to pass as the 2nd and 3rd argument.


-Brannon
-Brannon
(I can never resist...)

Yet another reason to use C++ IOstreams!

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Unfortunately, iostreams have performance issues.
or maybe programmers should start learning how to program.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Edited by - jenova on January 12, 2002 6:25:29 PM
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
IOstreams... yuck!
quote:Original post by Beer Hunter
Unfortunately, iostreams have performance issues.

Which are in large part balanced (or outweighed?) by their ease of use and superior design. And the performance hit isn''t that severe either.

Probably the only read advantage of C-style IO is with large text formatting problems (columns, tabs, yadda yadda).
float *foo;
foo = (float*)calloc(vertexnumber * 5,4);

fread(foo, 4, vertexnumber*5, mousefile);

works perfectly
"be fair! ... always"Zeusel
Run! Flee! The IOStream! It''s flooding over!
Ok, just had to say that. Sorry but this post contributes nothing but bad comedy. B-)

-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel

This topic is closed to new replies.

Advertisement