pack_fread to array?
Smok
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?

BAF

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

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.

CGamesPlay
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.

BAF

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

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.

Kitty Cat
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.

CGamesPlay

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.

Thread #555395. Printed from Allegro.cc