newing pointers

Started by
14 comments, last by Orpheum 23 years, 10 months ago
Though I am using fread, fopen and the standard C calls i just use fseek and sets it to the end of the file and gets the offset. This way I get the size of the file.

>The problem is that you don''t know ahead of time how many >structures there will be in the file, so you don''t know how >much room to allocate. There are two ways to do this:
>1) When you save your file, first write the number of >structures to the file. That way, when loading, you can first >load how many structures you expect, then allocate that much >space in memory.
>2) Use a dynamically-growing array class (i.e. std::vector) and >read until the end of the file.

Advertisement
Ack, the stupid firewall was timing out so I posted my last one twice... dunno why it shows my as being Annonymous Poster... anyway, thats not the problem either zyzzy. I think Claus may be right, but I guess we'll see when I get home.

Claus - I havent had a chance to try your idea, but what do you think about my idea? (Check the double post from Annonymous Poster).

Heres the important question tho... does my 'new' syntax look ok for an array of pointers?? Is there any special dereferrencing Im not doing??

Edited by - Orpheum on June 21, 2000 4:17:55 PM
There is no spoon.
Funny notation, but if your complier says ok =)...
WORLD myworld*; <- gives error in my msvc6 =)

normal notation is
WORLD *myworld;

MYSTRUCT *mystruct[ 256 ]; // creates a array with 256 pointers
mystruct = new MYSTRUCT; // is ok;<br><br>out.write((char*) &mystruct, sizeof(MYSTRUCT);<br>out.write((char*) *mystruct.bmp.bmbits, sizeof(*mystruct.bmp.bmbits); <br><br>as you said, you cant use sizeof with dynamic allocated memory. =)<br>And even if you could, you would not know how many bytes to read from the file when loading it.<br><br>there is a function for dynamic memory. <br><br>mem_bytes_allocated = _msize( memory ); // memory = pointer to memory<br><br>——— storing the struct —————-<br>So what you do is, like you started with:<br>out.write((char*) mystruct, sizeof(MYSTRUCT);<br><br>then, like Stoffel said, write number af bytes the bmbits points at, using a unsigned long.<br><br>ULONG mem_bytes_allocated = _msize( memory );<br>out.write((char*)&mem_bytes_allocated, sizeof(ULONG));<br><br>and then store the bitmap.<br>out.write((char*)mystruct->bmp.bmbits, mem_bytes_allocated); <br><br>——— loading the struct —————-<br>is "just" the reversed<br><br>mystruct = new MYSTRUCT; <br>out.read((char*) mystruct, sizeof(MYSTRUCT));<br><br>ULONG mem_bytes_needed;<br>out.read((char*)&mem_bytes_needed, sizeof(ULONG));<br><br>mystruct.bmp.bmbits = new char[mem_bytes_needed];<br>out.read((char*)mystruct->bmp.bmbits, mem_bytes_needed); <br><br>———————————————-<br><br>there may be missing some type casts, but the method is ok.<br>You can skip writing number of bytes in bmp.bmbits if you are able to calculate it from the bitmap header =)<br><br>UHHH, just found a drawback….If you have several structs in one file, you cant pick it out. Since each "struct" has a different size. You would need to load all structs at once…or search through the file a each new pick. Or you would need to create a extra index file/header telling file offset of each struct.<br><br>Or have i missing something/Understood what you want to do, wrong ??<br><br>Edited by - Claus Hansen Ries on June 21, 2000 6:24:50 PM
Ries
Sweet man, I think you may have hit the nail on the head... I will have to try _msize out as soon as I get home. Sorry about the f***ed syntax... I wrote that in a hurry. =) I have another array in the tilset header that contains the byte offset of each entry.
There is no spoon.
I may be wrong, but isn''t the whole point of using STL streams so that you can define your own operators?


ostream& operator<<(ostream& os, MyStruct& my_data)
{
os << my_data.iIndex;
os << my_data.fileinfo;
os << my_data.BMP;

int iIndex;
_filedata_t fileinfo;
BITMAP BMP;

return( os );
}

istream& operator>>(istream& is, MyStruct& my_data)
{
is >> my_data.iIndex;
is >> my_data.fileinfo;
is >> my_data.BMP;

return( is );
}

// special types

ostream& operator<<(ostream& os, _filedata_t& fileinfo)
{
// write code for this

return( os );
}

istream& operator>>(istream& is, _filedata_t& fileinfo)
{
// write code for this

return( is );
}

ostream& operator<<(ostream& os, BITMAP& BMP)
{
os << BMP.bmType;
os << BMP.bmWidth << BMP.bmHeight;
os << BMP.bmWidthBytes;
os << BMP.bmPlanes;
os << BMP.bmBitsPixel;

unsigned long number_of_bytes = BMP.bmWidthBytes * BMP.bmHeight;

os << number_of_bytes;
os.write(BMP.bmBits, number_of_bytes);

return( os );
}

istream& operator>>(istream& is, BITMAP& BMP)
{
is >> BMP.bmType;
is >> BMP.bmWidth >> BMP.bmHeight;
is >> BMP.bmWidthBytes;
is >> BMP.bmPlanes;
is >> BMP.bmBitsPixel;

unsigned long number_of_bytes = 0;

is >> number_of_bytes;

// dynamicly allocate memory
BMP.bmBits = (LPVOID) new char[number_of_bytes];

is.read(BMP.bmBits, number_of_bytes);

return( os );
}



Then you just write a MyStruct out and read it in using the >> and << operators. Here''s an example:


void do_something(fstream& file, MyStruct& my_data)
{
// write it out
file << my_data;

// read it in (should''ve seeked first though)
file >> my_data;
}



Once you write your own operators, it''s easy to use them in your code.




- null_pointer
Sabre Multimedia
Thanks so much for all your help guys! I should have enough time tonight to fix my problems. One more question for ya on this thread... Im reading in bitmaps made in paint (or photoshop) and then packing them (along with a wrapper struct) into one large file. My engine will then read in the large file and load each ''wrapped'' bitmap into an array of type wrapper. Is using the BITMAP struct in my wrapper correct? Should I be using DIB''s for files instead? What struct would be best to use? Thanks!!
There is no spoon.

This topic is closed to new replies.

Advertisement