Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » strange problem with packfiles

This thread is locked; no one can reply to it. rss feed Print
strange problem with packfiles
waldermort
Member #6,259
October 2005

I have 2 simple functions, 1 for saving the other for loading:

1void save_data()
2{
3 PACKFILE *file;
4 file = pack_fopen("default.set","wp");
5 if (!file)
6 return;
7 
8 for (int i=0;i<4;i++) {
9 // write the name
10 pack_fwrite(pData[0]<i>.name,15,file); // english name
11 pack_fwrite(pData[1]<i>.name,15,file); // chinese name
12 int win = atoi(pData[0]<i>.money);
13 pack_iputl(win,file); // money coverted to int
14 pack_iputl(pData[0]<i>.lev,file); // level
15 pack_iputl(pData[0]<i>.pic,file); // picture
16 pack_iputl(pData[0]<i>.scatterbrain,file); // scatterbrain
17 }
18 pack_iputl(SBall.pos,file); // speed position
19 pack_iputl(MBall.pos,file); // music position
20 pack_iputl(FBall.pos,file); // sound fx position
21 pack_iputl(language,file); // selected language
22 pack_iputl(stake,file); // selected stake
23 
24 pack_fclose(file);
25}
26 
27void load_data()
28{
29 char *pName[2][4] = {{"Micky Mouse","Pink Panther","Your Name?","John Doe"},
30 {"¹¶Ð¡Ñà","ħÍõ","ÄúµÄÃû×Ö£¿","ÃùÈË"}};
31 char *pType[2][5] = {{"Idiot","Beginner","Average","Advanced","Professor"},
32 {"±¿µ°","³õ¼¶","Öм¶","¸ß¼¶","ר¼Ò"}};
33
34 for (int i=0;i<2;i++) // language
35 for (int j=0;j<4;j++) // player
36 for (int k=0;k<5;k++) // level
37 pData<i>[j].level[k] = pType<i>[k];
38 
39 PACKFILE *file;
40 file = pack_fopen("default.set","rp");
41 if (!file) {
42 for (int i=0;i<2;i++) { // language
43 for (int j=0;j<4;j++) { // player
44 pData<i>[j].lev = 2;
45 pData<i>[j].name = pName<i>[j];
46 pData<i>[j].money = "50000";
47 pData<i>[j].pic = 0;
48 pData<i>[j].scatterbrain = true;
49 }
50 }
51 SBall.pos = 50;
52 MBall.pos = 127;
53 FBall.pos = 127;
54 }
55 else {
56 for (int i=0;i<4;i++) {
57 // write the name
58 char temp[15];
59 pData[0]<i>.name = (char *)malloc(15);
60 pData[1]<i>.name = (char *)malloc(15);
61 pData[0]<i>.money = (char *)malloc(15);
62
63 pack_fread(temp,15,file);
64 strcpy(pData[0]<i>.name,temp); // english name
65 
66 pack_fread(temp,15,file);
67 strcpy(pData[1]<i>.name,temp); // chinese name
68 
69 int win = pack_igetl(file);
70 itoa(win,pData[0]<i>.money,10); // money converted to C string
71 pData[0]<i>.lev = pack_igetl(file); // level
72 pData[0]<i>.pic = pack_igetl(file); // picture
73 pData[0]<i>.scatterbrain = pack_igetl(file); // scatterbrain
74 }
75 SBall.pos = pack_igetl(file); // speed
76 MBall.pos = pack_igetl(file); // music
77 FBall.pos = pack_igetl(file); // sound fx
78 language = pack_igetl(file); // selected language
79 stake = pack_igetl(file); // selected stake
80 }
81 SBall.y = 218;
82 SBall.x = get_ball_pos(100,SBall.pos);
83 MBall.y = 263;
84 MBall.x = get_ball_pos(255,MBall.pos);
85 FBall.y = 308;
86 FBall.x = get_ball_pos(255,FBall.pos);
87 
88 pack_fclose(file);
89}

Now this works exactly as it should do, with one strange problem. If I build the exe, export all the files to a seperate directory and run (including the packfile), it works. If I delete te packfile and run the exe, it crashes. Now I have included a check in the code, so it should not be doing this, it should simply load the default values.

A little twist to this problem, if I do exactly the same from within MSVC (i.e. delete the generated packfile) it works. Due to this I am unable to debug it and find out where the problem lies.

[EDIT]
Don't you just hate it when you spend hours trying to find the problem, make a post, then 2 minutes later fix it >:(

I did something a little stupid, I deleted the .dat file instead of the .set file. A little stupid I know, but they share the same icon, which I must now go and add code for to give it it's own icon.

Elverion
Member #6,239
September 2005
avatar

Maybe you should change your check to "if file exists" rather than !packfile_open. If the file is not found, it might return some non-zero number still and could be why you are having problems. It says it should return NULL in the manual, but who knows...

--
SolarStrike Software - MicroMacro home - Automation software.

Evert
Member #794
November 2000
avatar

Quote:

If the file is not found, it might return some non-zero number still and could be why you are having problems.

It doesn't return a number, it returns a pointer to a PACKFILE struct.

Quote:

It says it should return NULL in the manual, but who knows...

If it doesn't, it's a bug that should be fixed (but it actually does return NULL).

Quote:

I did something a little stupid, I deleted the .dat file instead of the .set file. A little stupid I know, but they share the same icon

You're not hiding the extensions, are you? :o

waldermort
Member #6,259
October 2005

Quote:

You're not hiding the extensions, are you?

Now that would be a good excuse, a good way to save a little face perhaps, but the answer is no :-[

I can't explain why I did it, perhaps I automaticaly deleted the last file in the list (which should be the last file created/added). But obviously i was wrong. This'l teach me to be a little more careful in the future.

One good thing to come out of this is noticing my failiure to code a check for the datafile.

I am now trying to find out how to manipulate the registry as to add an icon for these .set files.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I've associated allegro programs with extensions by simply double clicking on them and when the "Open With" thing comes up, click on Browse to get to the program and make sure the "Always open with this program" is checked.

They all watch too much MSNBC... they get ideas.

waldermort
Member #6,259
October 2005

That would work, but only on your system. I don't want the users of my programs to be making these mistakes (not that they should be in those folders anyway). I just add a few lines of code to set the registry. Now those files have their own Icons and settings on all systems.

Go to: