Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro Save/Load

This thread is locked; no one can reply to it. rss feed Print
Allegro Save/Load
thirdy
Member #8,409
March 2007
avatar

Hi I'm doing allegro in devc++ and I got this error. I have no experience in doing file i/o, please help.

313 C:\Dev-Cpp\Projects\Project Jem\Mainmenu.h cannot convert `CHARSTATS' to `const void*' for argument `1' to `long int pack_fwrite(const void*, long int, PACKFILE*)'

----------Here's my code---------

int save()
{
long int x=sizeof(CHARSTATS);
PACKFILE *file;

file = pack_fopen("file.sav", "wb");

pack_fwrite(jemstats, x, file);

pack_fclose(file);
return 0;
}

int load()
{
PACKFILE *file;

if (file_select("Load Player (*.sav)", fname, "sav") == 0)
return 1;

file = pack_fopen(fname, "rb");

pack_fread(jemstats, sizeof(CHARSTATS), file);

pack_fclose(file);
return 0;
}

LennyLen
Member #5,313
December 2004
avatar

How is jemstats defined?

And please use the code tags to make your post more legible.

thirdy
Member #8,409
March 2007
avatar

ok, sorry

1int save()
2{
3long int x=sizeof(CHARSTATS);
4PACKFILE *file;
5 
6file = pack_fopen("file.sav", "wb");
7 
8pack_fwrite(jemstats, x, file);
9 
10pack_fclose(file);
11return 0;
12}
13 
14int load()
15{
16PACKFILE *file;
17 
18if (file_select("Load Player (*.sav)", fname, "sav") == 0)
19return 1;
20 
21file = pack_fopen(fname, "rb");
22 
23pack_fread(jemstats, sizeof(CHARSTATS), file);
24 
25pack_fclose(file);
26return 0;
27}

---jemstats definition----

typedef struct {
       
       int Attack, Hp, Defense, Xp, Sword, Special;
       int Level;
       char Name[10]; 
       
       
       }CHARSTATS;

DanielH
Member #934
January 2001
avatar

1. For compatability, don't use fread, and fwrite. Instead load, save the individual variables.
2. get into the habit of Error checking always. How do you know the file opened before you started reading or writing to it?
3. Proper spacing. (This is debatable) But the most part is to line things up to make the code more readable.

1void pack_fput_charstat( CHARSTATS &cs, PACKFILE *pfile )
2{
3 int i = 0;
4 
5 pack_iputl( cs.Attack, pfile );
6 pack_iputl( cs.Hp, pfile );
7 pack_iputl( cs.Defense, pfile );
8 pack_iputl( cs.Sword, pfile );
9 pack_iputl( cs.Special, pfile );
10 pack_iputl( cs.Level, pfile );
11 
12 for ( i = 0; i < sizeof( cs->Name ); i++ )
13 {
14 pack_putc( cs.Name[ i ], pfile );
15 }
16}
17 
18 
19void pack_fget_charstat( CHARSTATS &cs, PACKFILE *pfile )
20{
21 int i = 0;
22 
23 cs.Attack = pack_igetl( pfile );
24 cs.Hp = pack_igetl( file );
25 cs.Defense = pack_igetl( pfile );
26 cs.Sword = pack_igetl( pfile );
27 cs.Special = pack_igetl( pfile );
28 cs.Level = pack_igetl( pfile );
29 
30 for ( i = 0; i < sizeof( cs->Name ); i++ )
31 {
32 cs.Name[ i ] = pack_getc( pfile );
33 }
34}
35 
36int save( const char *filename )
37{
38 PACKFILE *file = NULL;
39 
40 file = pack_fopen( filename, "wb" );
41 
42 if ( file != NULL )
43 {
44 // do all saving here
45 pack_fput_charstat( jemstats, file );
46 
47 pack_fclose( file );
48 return 0;
49 }
50 
51 return -1;
52}
53 
54int load()
55{
56 char filename[ 1024 ] = "";
57 PACKFILE *file = NULL;
58 
59 if ( file_select( "Load Player (*.sav)", filename, "sav" ) == 0 )
60 {
61 return -1;
62 }
63 
64 file = pack_fopen( filename, "rb" );
65 
66 if ( file != NULL )
67 {
68 // do all loading here
69 pack_fget_charstat( jemstats, file );
70 
71 pack_fclose( file );
72 return 0;
73 }
74 
75 return -1;
76}

LennyLen
Member #5,313
December 2004
avatar

Quote:

---jemstats definition----

I still see no definition of jemstats. The definition of the datatype it is an instance of is not the same thing.

BTW, do you actually code without indentation, or did you just copy/paste the code from your earlier post and put code tags around it?

Quote:

1. For compatability, don't use fread, and fwrite.

The compatibility issues with the traditional fread() and fwrite() functions are not present in the pack_ versions.

Quote:

Instead load, save the individual variables.

Why loop through the loading and saving of members of structs when allegro has a function for reading/writing blocks of them in one line? He used the right functions for the job, just incorrectly.

gnolam
Member #2,030
March 2002
avatar

LennyLen said:

The compatibility issues with the traditional fread() and fwrite() functions are not present in the pack_ versions.

Actually, they are. Endianness is resolved, but data padding is still compiler- and platform-dependent.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

thirdy
Member #8,409
March 2007
avatar

Finally I've done it thanks! I've just started programming//1st yr IT student
I have no idea what is printf() 4 mos ago.
Just one last problem, how do exactly use file_select()?

what's wrong with this?

if ( "C:/Dev-Cpp/Projects/Project Jem", "FILE.TXT", "TXT" ) == 0 )
    { 
       allegro_message("error loading file.txt");      
    }

BTW, do you any good site that gives free domains? My site, thirdy.tk is full of ads.
Thanks again!

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

Endianness is resolved,

Not for raw pack_fread() / pack_fwrite() for all I know. These functions read / write raw byte streams, without any endianness considerations whatsoever. They have to, since all they get is a void*, which could contain anything - strings or char arrays, in which case the byte order needs to be preserved as-is, or larger int types where the byte order needs to be reversed in groups of 2 or 4, if the system's endianness requires it.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

gnolam
Member #2,030
March 2002
avatar

Ah, yes, I was thinking about the pack_i* and pack_m* functions. My bad. :)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

BAF
Member #2,981
December 2002
avatar

Quote:

if ( "C:/Dev-Cpp/Projects/Project Jem", "FILE.TXT", "TXT" ) == 0 )

You have an extra ) (at least without ->) and a missiong function call / (.

Go to: