PhysFS not reading file properly?

Started by
3 comments, last by TFS_Waldo 18 years, 1 month ago
Okay, I got PhysFS to compile. But now, I am having trouble reading a file. I have a .zip file called "ziptest.zip" containing "test.bmp" (just regular .bmp file). Now, when I get the file size from the .zip file, it returns "3126", which is correct. The original .bmp is about 3.2KB. Well, here is the code:

// MyTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int main(int argc, char* argv[])
{
	// TODO: Enter main program code here
	PHYSFS_init(NULL);     // Initialize library

	PHYSFS_addToSearchPath("ziptest.zip", 1);   // Add file to search path


	cout << "Checking file existence...\n";
	if (! PHYSFS_exists("test.bmp"))     // Check to see if file exists
	{
		cout << "File test.bmp does not exist.\n";   // Print error and exit

		return -1;   // Return error
	}

	cout << "Opening file...\n";
	// Attempt to read the file now
	PHYSFS_file *myFile = PHYSFS_openRead("test.bmp");   // Open file for reading
	if (myFile == NULL)    // File handle invalid
	{
		cout << "Invalid file handle!\n";

		return -1;
	}

	cout << "Getting file length...\n";
	PHYSFS_sint64 file_size = PHYSFS_fileLength(myFile);    // Get file size

	cout << (int) file_size << "\n";
	// Read the file into a buffer
	cout << "Allocating read buffer...\n";
	char *myBuf = new char[file_size];   // Allocate read buffer
	cout << "Reading file into buffer...\n";
	int length_read = PHYSFS_read(myFile, myBuf, 1, file_size);   // Read file


	cout << "Shutting down...\n";
	// Close the open file and de-initialize PhysFS
	PHYSFS_close(myFile);
	PHYSFS_deinit();


	cout << "Writing to C:\\test.bmp...\n";
	ofstream out("C:\\test.bmp");   // Open the file
	
	out << myBuf;    // Write buffer to file

	out.close();   // Close the file


	cout << "Application finished!\n";

	return 0;
}

The problem is that when I write the data that was read from the .zip to "C:\test.bmp" the file size of "C:\test.bmp" is only 4 bytes. Am I doing something wrong? Am I not reading/writing the file correctly?
:==-_ Why don't the voices just leave me alone?! _-==:
Advertisement
This might not have any effect on it at all, but try changing the char* to a byte*.
Rob Loach [Website] [Projects] [Contact]
The << was meant for text data, as in null terminated character data. Apparently its only getting 4 bytes into the bitmap before encountering a null character and thinking that's all there is to write. You'll need to use a function meant for writing binary data.
I like the DARK layout!
Well, what functions should I use then? I'm sorry, but I haven't worked with plain console apps. in a while. I'm used to using "CreateFile", "ReadFile", "WriteFile", etc. I just thought that maybe I could do it with 'ofstream'. I mayi give "WriteFile" a try though. =)
:==-_ Why don't the voices just leave me alone?! _-==:
Okay, nevermind. I got it!!! I used 'out.write(myBuf, file_size)' and it worked just fine. Thanks to both of you! =)
:==-_ Why don't the voices just leave me alone?! _-==:

This topic is closed to new replies.

Advertisement