Sign in to follow this  

What is wrong with this code?

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i].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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i].FileName);

return true;
}

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

Share this post


Link to post
Share on other sites

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this