Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » pack_fread to array?

Credits go to BAF for helping out!
This thread is locked; no one can reply to it. rss feed Print
pack_fread to array?
Smok
Member #6,662
December 2005

1//objects.h
2// define object
3class object{
4 public:
5 bool crossable[7][7][8]; //dosn't matter - just some strange data.
6 bool ontop[7][7];
7 short image[7][7];
8 };
9// define objects as pointer to object
10object * objects;
11 
12// load all objects from file to array, which will be made from 'objects'
13void load_objects(char filename[32]){
14 params=pack_fopen(filename, "rb");
15 count=(file_size(filename)/sizeof(object)); //how many objects are in file?
16 objects = new object[count]; //create array from objects
17 int i=0;
18 while(!pack_eof(params)){
19 pack_fread(objects<i>,sizeof(object),params);
20 i++;
21 }
22 }

Dev-Cpp 4.9.9.2; Allegro 4.2.0

Error:
cannot convert `object' to `void*' for argument `1' to `long int pack_fread(void*, long int, PACKFILE*)'

Where my thinking is wrong? Why doesn't it work?

I'm just asking... :D

BAF
Member #2,981
December 2002
avatar

You would have to cast objects[x] to (void *). But, due to endianness and such, you shouldn't store whole structures like that. You should manually write/read each variable.

Smok
Member #6,662
December 2005

You mean doing sth like this?

pack_fread((void *) objects<i>,sizeof(object),params);

It dosn't work:

Cannot convert `*((+(((unsigned int)i) * 540u)) + objects)' from type `object' to type `void*'

I don't care about endianness and such, cause this will be object-editor tool for my use only.

Thanks anyway :]

[EDIT]
Doing it your way would be quicker. Thanks.

I'm just asking... :D

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

I don't care about endianness and such

You should care about "and such", because it includes "compiler version and even compile itself". I.e. if you do that dont' expect your save games to work in the next version of your program.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

BAF
Member #2,981
December 2002
avatar

I don't endorse what you are doing, writing the whole class to file, but, it would be pack_fread((void *)&objects<i>, sizeof(object), blah); .

CGamesPlay
Member #2,559
July 2002
avatar

But it's important to know that you can safely write entire arrays to a file at one time. That means:
pack_fread(objects, sizeof(object) * number_of_objects, blah);(To quote BAF's example.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

But it's important to know that you can safely write entire arrays to a file at one time.

Not if the structs/classes contain pointers to anything (which can include member functions). Running it once can have a different pointer than when you run it next time.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

CGamesPlay
Member #2,559
July 2002
avatar

Well, I didn't say structures. I said arrays. We've already established that it isn't safe to write structures directly to files, with or without pointers.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Go to: