fread()'ing into a vector

Started by
6 comments, last by Crispy 21 years, 8 months ago
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
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
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]
daerid@gmail.com
Without STL (what am I saying...)
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

This topic is closed to new replies.

Advertisement