- Online Community Forums » Programming Questions » strange problem with packfiles

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

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

1void save_data()
3 PACKFILE *file;
4 file = pack_fopen("default.set","wp");
5 if (!file)
6 return;
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
24 pack_fclose(file);
27void load_data()
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 {"±¿µ°","³õ¼¶","Öм¶","¸ß¼¶","ר¼Ò"}};
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];
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);
63 pack_fread(temp,15,file);
64 strcpy(pData[0]<i>.name,temp); // english name
66 pack_fread(temp,15,file);
67 strcpy(pData[1]<i>.name,temp); // chinese name
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);
88 pack_fclose(file);

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.

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.

Member #6,239
September 2005

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.

Member #794
November 2000


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.


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


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

Member #6,259
October 2005


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

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.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

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: