// 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;
}
PhysFS not reading file properly?
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:
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?
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.
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. =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement