What is wrong with this code?

Started by
7 comments, last by Kryptus 19 years, 5 months ago
I researched into Quake II PAK files and I tried out some code, but I must have a memory leakage or something... something is not right. I keep getting that dialog box saying "This program has encountered a problem." Here's the code: typedef struct { char Signature[4]; int DirOffset; int DirLength; int NumFiles; } pakHeader; typedef struct { char FileName[56]; int FilePosition; int FileLength; } pakEntry; bool OpenPAK(HWND hWnd, char FileName[]) { // Open a PAK file FILE *file; pakHeader Header; pakEntry *Entry; int NumFiles; file = fopen(FileName, "rb"); fread(&Header, sizeof(Header), 1, file); if (strncmp(Header.Signature, "PACK", 4)) fclose(file); fread(&Header.DirOffset, sizeof(int), 1, file); fread(&Header.DirLength, sizeof(int), 1, file); NumFiles = Header.DirLength / sizeof(Header); Entry = new pakEntry[NumFiles]; fseek(file, Header.DirOffset, SEEK_SET); fread(Entry, sizeof(pakEntry), NumFiles, file); for (int i = 0; i < NumFiles; i++) SendMessage(hListView, LB_ADDSTRING, 0, (LPARAM) Entry.FileName); return true; } Basically, I want this program to load a PAK file, and display the contents in a listbox that I have placed on the form. I'm guessing the problem lies with the Entry pointer and that the FOR loop and SendMessage function are having problems. Please help fix this problem.
Advertisement
Just glancing over the code quickly, you forgot the opening bracket of the for statement at the bottom of the code.
Eddie Fisher
Offhand, I would suggest that the fact that you're reading Header as a whole, and then rereading the DirOffset and DirLength attributes, might be a problem.
Yeah. I'll have to experiment a bit with all the objects :S Oh and to the first reply... You don't need the brackets if you have only one line of code following.
 if (strncmp(Header.Signature, "PACK", 4))fclose(file);fread(&Header.DirOffset, sizeof(int), 1, file);


It might just be that I haven't used it enough, but it looks to me like you're reading from a dead file. I was reading over the file format description at Wotsit's, and...the signature of the header has to be "PACK", right? Maybe you wanted to put if(strncmp(Header.Signature,"PACK",4)<0) fclose(file)?

I don't know, don't really have much experience with stuff like this. Looks to me like it might generate an error if you close a file and then try to read from it, though.


EDIT: Also, what exactly is SendMessage sending? I don't see an hListView in your code here, so I assume it's global. I've never seen it before, but from what I can gather off the MSDN, it looks like you're just adding the filename of each .pak file to a list of strings...so that wouldn't be a problem, right? Looks like you already explained that...

EDIT (again): I think your code to get the number of directories (NumFiles) may be wrong. The docs say to divide by 64 to get the number of directory files, but you divide by the sizeof the header, which is supposed to be only 12 (check). Really just trying to find anything that could possibly be a problem, and that may present itself if you find file errors in the future.

Just trying to learn.
Things change.
Over the last few minutes I've been revising it. I found the error to be in these lines:

fread(&Header.DirOffset, sizeof(int), 1, file);
fread(&Header.DirLength, sizeof(int), 1, file);

At least I think it's there. About the handle to the listview, it's got nothing to do with that because I can add normal strings and numbers, etc.
Quote:Original post by Kryptus
Over the last few minutes I've been revising it. I found the error to be in these lines:

fread(&Header.DirOffset, sizeof(int), 1, file);
fread(&Header.DirLength, sizeof(int), 1, file);

At least I think it's there. About the handle to the listview, it's got nothing to do with that because I can add normal strings and numbers, etc.


Upon rereading your original post I did figure out just what the heck hListView was :P.

Did you check the line right above those two (mentioned in last post), about closing the file and then reading from it? Just what kind of error did you find?
Things change.
It was that Windows 2K/XP error dialog saying: "This program has encountered an error." and then it has a Debug button a Don't Send and a Send button, for sending the error to Microsoft.

I think I've nearly solved the problem. By focusing more on the PAK file guide on http://www.wotsit.org I have come closer to a solution. After removing that code and dividing the directory length by 64 it now displays the files in the PAK file. Also adding to the problem was the fact that the PAK file generator I used to make PAK files wasn't making them properly. I downloaded Pak Explorer and then tested my program and it now seems to work! I'm just going to further test it before I start cheering and parading.
Alright my code finally works now for opening pak files! All because of some corrupt PAK files and dodgy code!

typedef struct
{
char Signature[4];
int DirOffset;
int DirLength;
} pakHeader;

typedef struct
{
char FileName[56];
int FilePosition;
int FileLength;
} pakEntry;

bool OpenPAK(HWND hWnd, char FileName[])
{
// Open a PAK file
FILE *file;
pakHeader Header;
pakEntry *Entry;
int NumFiles;

file = fopen(FileName, "rb");

fread(&Header, sizeof(Header), 1, file);
if (strncmp(Header.Signature, "PACK", 4))
fclose(file);

NumFiles = (Header.DirLength / 64);
Entry = new pakEntry[NumFiles];

fseek(file, Header.DirOffset, SEEK_SET);
fread(Entry, sizeof(pakEntry), NumFiles, file);

for (int i = 0; i < NumFiles; i++)
SendMessage(hListView, LB_ADDSTRING, 0, (LPARAM) Entry.FileName);

return true;
}

This code works for opening Quake 2 PAK files and displaying them in a listbox!

This topic is closed to new replies.

Advertisement