Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ equivalant fread function

Started by xg0blin Dec 19, 2002 at 1:48 PM 4 replies 700+ views
Original Post
xg0blin
xg0blin
Is there like a c++ equivalent to the fread function? Any kind of stream I can use to get the same effect. I realize ifstream has a read and readsome function, but the arguments are
ifstream::read(char *, size). I don''t want to load all char''s, I''d like to be able to do something more in line with the binary io available with fread, where they use the void * (maybe there is a templated ifstream function somewhere or something?). Anyone know the answer to this question?
Jason2Jason
Jason2Jason
i dont knwo much about the ifstream way, but i use fread and its co-functions (fwrite, fopen, etc) in C++ all the time. I''m not sure if i''m ment to or not, but hey!

-J
daerid
daerid
What''s wrong with using char* ''s? Just cast ''em back to the correct type.


  

struct vec
{
float x;
float y;
float z;
};

ifstream str("file.dat",ios::binary);
vec v;
str.read(reinterpret_cast<char*>(&vec),sizeof(vec));

daerid@gmail.com
xg0blin
xg0blin
I use those functions as well, but I am more comfortable with the c++ way. Easier to read to my eyes. I like the streams with the neat little brackets. Just curious I guess, not something I have to have.
xg0blin
xg0blin
That''s all fine and everything, I just think it is kind of a pain to cast everything after you load it. What if I was trying to load a .3ds model or something?
Oluseyi
Oluseyi
quote:
Original post by xg0blin
That''s all fine and everything, I just think it is kind of a pain to cast everything after you load it. What if I was trying to load a .3ds model or something?

You''re not casting to char after loading. Your casting the pointer to your data destination to a char pointer, such that pointer operations occur a byte at a time.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.