Archived

This topic is now archived and is closed to further replies.

fread()'ing into a vector

This topic is 5597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, is something like this
  
std::vector<object>o;
fread(o, 100, sizeof(object), filehandle);
  
possible? The above example produces an error, naturally, but the prospect of having to loop through a huge amount of items, read them one by one, and then add to the vector through a transition object doesn''t seem at all welcoming either... Thanks, Crispy

Share this post


Link to post
Share on other sites
I think so.

something like this?


    
class object
{
// object class

friend std::istream& operator>>(std::istream&,object&); // <- you'd have to write that function

};

std::vector<object> ovec;

std::ifstream file;
file.open("objectfile.bin",std::ios::in);
if(!file.fail())
{
std::istream_iterator<object> begin(file);
std::istream_iterator<object> end;
std::copy(begin,end,std::back_inserter(ovec));
file.close();
}



[edited by - daerid on August 15, 2002 7:57:00 PM]

Share this post


Link to post
Share on other sites
Without STL (what am I saying...)

vector<object> o
fread( (char*)&o[0], 100, sizeof( object ), filehandle );


This will only work with vector because it is guaranteed to be allocated contiguous memory. It will also only work if your object is a POD-struct (ie. what C programmers think of as a struct), without virtual functions, private members nor member variables needing copy constructors (and so on).

For a vector, &vec[0] really is a pointer into a C-compatible array (here, an object*).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
This will only work with vector because it is guaranteed to be allocated contiguous memory.

Actually, it''s not. But it should be, and probably will be in the future. http://www.gotw.ca/publications/mill10.htm.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
puzzled--Scott Meyer''s "Effective STL" says that it is guaranteed contiguous. IANALL (I am not a language lawyer), so...is it or isn''t it?

I re-emphasize Fruny''s point: this only works with PODs.

Share this post


Link to post
Share on other sites
I'm assuming they're POD structs or POD unions or something that can be (de)serialized in such a manner safely.

I'm assuming also that your vector implementation uses contiguous memory. It's not required behaviour (the performance characteristics imply it but alternative implementations are possible) though it no doubt should be, and will be in the future.

In which case it'd be a two step process.

First, resize your vector to the required size. This way, the vector knows how many elements it contains, and ensures that there's memory allocated for all of them.

Second, read them into the vector; use the address of the first element as the argument to fread().

Of course, iostream overloads would be much more in the spirit of C++.


[edited by - DrPizza on August 16, 2002 1:00:06 AM]

Share this post


Link to post
Share on other sites
Mkay - thanks guys a lot! Lucky for me, I only have a bunch of structs I want to read in (POD''s, gotta run a search on that). Another vector question though - I''m biased towards holding pointers to objects in vectors rather than actual objects. Is it possible to allocate a bunch of objects in a vector (that has previously been resize()''d ) without iterating through them one by one? The target of this code is a bsp loader, and the vector is supposed to store faces, nodes, etc. that can be pretty abundant in a huge level (>20000), so I''m guessing looping''s not the best solution here.

Crispy

Share this post


Link to post
Share on other sites