HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Small problem with SFmpqapi

12-24-2006, 12:27 AM#1
wyrmlord
I decided to mess around with the SFmpqapi, and after some debugging everything is working fine, except when I take a look at a script taken out from a map, I find 4 weird characters added to the end of the file. I would like to know what is causing the error, here's my code if anyone needs to take a look at it. (C++) And if it helps at all, I'm using Microsoft's Visual C++ compiler.
Hidden information:
Code:
char* GetMPQFileContents(char* MPQ, char* Filename)
{
	HANDLE mpq;
	HANDLE file;

	if(!SFileOpenArchive(MPQ, 0, 0, &mpq))
		printf("Failed to open archive\n");
	if(!SFileOpenFileEx(mpq, Filename, 0, &file))
		printf("Failed to open file\n");

	DWORD size = SFileGetFileSize(file,0);

	printf("File size is: %i\n", (int)size);

	char *buffer = new char[size];

	SFileReadFile(file, buffer, size, &size, NULL);

        //printf("%s", buffer);

	SFileCloseArchive(mpq);

	return buffer;
}
12-25-2006, 03:16 PM#2
Zoxc
printf expects to find a null terminator at the end of the buffer. (else it will print the text until it finds the first null terminator in memory (memory printed qualifies at wierd chars))

Just do
char *buffer = new char[size+1];
that will add a 0x00 byte to the end.
12-26-2006, 02:25 AM#3
wyrmlord
Thanks Zoxc. I knew it was something simple, I just didn't know what.

EDIT: Having buffer = new char[size+1] didn't work, but after reading from the file and setting buffer[size-1] = '\0' worked.
12-27-2006, 02:30 PM#4
xttocs
If your setting the value at (size - 1) to null then you are losing one byte of the data. Also, if you are just reading data from MPQs, I would recommend linking to Storm.dll and using the Blizzard APIs instead. There is actually a routine that allows you to load a file all at once into a buffer with additional padding at the end, which would be perfect for this case.
12-27-2006, 02:55 PM#5
wyrmlord
I'm not sure what I'm going to be doing yet with MPQ files. Anyways, where can I find the Blizzard APIs to use with Storm.dll?

EDIT: Also, reading files with the SFmpqapi also works with setting the buffer to the size+1 and then setting buffer[size] to null.
01-15-2007, 04:54 PM#6
xttocs
http://www.wc3campaigns.net/blizzter...rm.dll_exports