std::vector<object>o;
fread(o, 100, sizeof(object), filehandle);
fread()'ing into a vector
Hi,
is something like this
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
I think so.
something like this?
[edited by - daerid on August 15, 2002 7:57:00 PM]
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]
Without STL (what am I saying...)
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 ]
vector<object> ofread( (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 ]
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 ]
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.
I re-emphasize Fruny''s point: this only works with PODs.
Hmmm, you are right, my copy of the Standard doesn''t mention that (maybe in a TC?).
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
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]
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]
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
Crispy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement