can't seem to read binary file using fread

Started by
1 comment, last by adi0149 14 years, 7 months ago
I know there's already been a discussion about this topic but i'm very new to programming and i couldn't make much of it. Anyway... i'm trying to write some data into a file and then read it and use it later. Here's the code: (omg... don't know how to format it) struct ParticleProperties { int shape; float red, green, blue; float dimensions[3]; float position[3]; float orientation[3]; float velocity[3]; float acceleration[3]; float forcedir[3]; float forcemag; int density; }; and: ParticleProperties read,write,load; FILE *_file,*stream,*output; vector<ParticleProperties> LoadedFromFile; fopen_s( &stream, "D:\\Scoala\\Licenta\\Back-up\\2009.09.14.ReadFiles\\Direct3D9MFC\\Direct3D9MFC\\saves\\test1.bin", "wb+" ); for (vector<ParticleProperties>::iterator write=vPartProp.begin(); write!=vPartProp.end(); write++) { fwrite(&write,sizeof(ParticleProperties),1,stream); } fclose( stream ); fopen_s( &_file,"D:\\Scoala\\Licenta\\Back-up\\2009.09.14.ReadFiles\\Direct3D9MFC\\Direct3D9MFC\\saves\\test1.bin","rb+" ); fseek (_file, 0, SEEK_END); long int size = ftell(_file); rewind(_file); long int pos = ftell(_file); while (pos < size) { fread(&read,sizeof(float),sizeof(ParticleProperties)/sizeof(float),_file); LoadedFromFile.push_back(read); pos = ftell(_file); } fclose( _file ); fopen_s( &output, "D:\\Scoala\\Licenta\\Back-up\\2009.09.14.ReadFiles\\Direct3D9MFC\\Direct3D9MFC\\saves\\test2.bin", "w+t" ); for (vector<ParticleProperties>::iterator load=LoadedFromFile.begin(); load!=LoadedFromFile.end(); load++) { fprintf( output, "%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d\n",load->shape, load->red, load->green, load->blue, load->dimensions[0],load->dimensions[1],load->dimensions[2],load->position[0],load->position[1],load->position[2], load->orientation[0],load->orientation[1],load->orientation[2],load->velocity[0],load->velocity[1],load->velocity[2], load->acceleration[0],load->acceleration[1],load->acceleration[2],load->forcedir[0],load->forcedir[1],load->forcedir[2], load->forcemag,load->density); } fclose( output ); I'm using the "output" to write the data in text format to actually see what i read. anyway, it seems that "write" has the correct values while writing them, but once it gets out of the "for" loop, it seems to take arbitrary values. (i don't really know if that is an issue or not since the data had already been written to the file). however, the "read" has complitely strange values (most of them zero) that have nothing to do with what i wanted to write and then read. I would like someone to give some advice on how to make this work, but do it clearly please, cause i'm n00b. :)
Advertisement
First, read the FAQ (upper-right corner of this screen) about using and [ source] formats.<br><br>One thing I notice is that you have ParticleProperities structures named "write" and "load." You then locally define "write" and "load" as iterators. That may be your biggest problem. The iterator is not the structure you intended.<br><br>An iterator is a pointer, so your fwrite is writing garbage from the memory location where the pointer is stored. You need to dereference the pointer before you write anything out. Something like:<br><pre><br>for (vector&lt;ParticleProperties&gt;::iterator write=vPartProp.begin(); write!=vPartProp.end(); write++)<br>{<br> ParticleProperties temp = *write;<br> fwrite(&temp,sizeof(ParticleProperties),1,stream);<br>}<br></pre><br><br>You give no indication what vPartProp is. Looks like a vector but no idea what you've pushed into it.<br><br>In general, if you're opening a file for just writing, use "wb" rather than "wb+" That doesn't affect your problem - just a suggestion.<br><br>

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

thx for help. i'll be putting your advice in action in a few minutes. i'll get back to you if it worked or not.

...and yes, vPartProp is a vector of type ParticleProperties, having stored some data about some particles :)

EDIT:
Finally... it works. Thank you very much! I've been trying to figure this out for a few hours now... Thx :)

This topic is closed to new replies.

Advertisement