Archived

This topic is now archived and is closed to further replies.

fried_chicken

binary files

Recommended Posts

fried_chicken    122
I got a c++ question. I made a flash game, but I lost the fla for it. I need to change the FPS, so I''m writing a simple program in c++ to do this. I have never used binary files with c++ and I dont really know much about bits and bytes. The first three bytes of a flash file is "FWS" I want to check this to make sure the file is a flash file. When I try to read it in from the file, I get "FWSyyyy" Here is the code
ifstream swf;
char *h=(char*)malloc(sizeof(char)*3);
swf.read(h,sizeof(char)*3);
 

Share this post


Link to post
Share on other sites
Maega    707
First off, since you''re using C++, use new. It''s alot less error prone in my opinion.

You also might want to make sure you''re opening it in binary too (which I hope you are :D)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Hi, just a guess (since I don''t know the particular API you''re using from personal experience) but I think the problem is that you''re only allocating enough space for 3 characters. You need to allocate space for 4 characters and put a terminating ''\0'' character in the last position. Otherwise there is no way to know where the string ends, and it will display garbage characters until it encounters a byte with value 0.

Also, be careful that you are reading the right number of characters. If the ifstream class is templated to read a certain number of characters (as opposed to bytes) than you don''t want to multiply by "sizeof(char)" when you call "read".

Share this post


Link to post
Share on other sites
Spudder    385
If you have a char array which you are reading the data into a found using memset() to set the array elements to 0 can help matters - memset(Array, 0, sizeof(Array)). This way if you allocate an buffer which is bigger than the actual data the extra elements doesn''t show up when you output the buffer - instead of "FWSyyyy" you will only have "FWS"

My 2D game engine

Share this post


Link to post
Share on other sites
Android_s    164
Not sure if this is going to help you. I copied this from my TGA loader. This part reads the header of the TGA.


unsigned char tgaHeader[6]; // Declare an array to store the header info in


ifstream inFile; // Declares an ifsteam object


// Opens a file in binary mode, write-protect and if non-exist it won't create a file and move on ;)

inFile.open(Filename, ios::in | ios::nocreate | ios::binary);
if(inFile.fail()) // If the file dosn't exist...

{
return NULL; // Abort

}

inFile.seekg(12, ios::beg); // Jumps 12 bytes into the file from the beginning

inFile.read(tgaHeader, sizeof(tgaHeader)); // Read sizeof(tgaHeader) bytes from inFile and stor it into tgaHeader




I am not sure about your code but i would think it would look something like this:

unsigned char header[3]; // Create space for the 3 bytes


ifstream inFile; // Declare the ifsteam object


// I assume you already knew this line, but i'll add it anyway ;)

inFile.open("theSWFFile.swf", ios::in | ios::nocreate | ios::binary);

inFile.seekg(ios::beg); // Makes sure we're at the beginning of the file


inFile.read(header, sizeof(char) * 3); // This SHOULD read the first 3 bytes of the file, and the result should be header[0] = 'S', header[1] = 'W' and header[2] = 'F'...




Hope it helps (and is correct code... )

[edited by - Android_s on January 6, 2004 10:31:30 AM]

Share this post


Link to post
Share on other sites
fried_chicken    122
I tried that stuff. It didnt work... Here''s my whole function.


bool LoadSwf(HWND hwnd)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ifstream swf;
char *h=(char*)malloc(sizeof(char)*3);

//fill in the file load struct

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "
Flash Files (*.swf)\0*.swf\0All File(*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
ofn.lpstrDefExt = "
swf";

if(!GetOpenFileName(&ofn))
{
MessageBox(hwnd,"
Failed to open file!","Error!",IDOK);
return false;
}

swf.open((LPCSTR)szFileName,ios::binary);
if(!swf)
{
MessageBox(hwnd,"
Failed to open file!","Error!",IDOK);
return false;
}
swf.seekg(ios::beg);
swf.read(h,sizeof(char)*3);
//check the string

MessageBox(hwnd,(LPCSTR)h,"
Debug",IDOK);
free(h);
swf.close();
return true;
}


In the flash file format doc it says


The header beings with the three-byte Signature 0x46, 0x57, 0x53 (“FWS”)


but I keep getting a bunch of garbage after the FWS.

Share this post


Link to post
Share on other sites
psykr    295
Once again.. your buffer has to be FOUR chars long. Three to hold ''S'', ''W'', and ''F'', and then the last terminating null byte. Without the trailing zero, any function trying to read the string will just keep reading past the ''SWF''.

Share this post


Link to post
Share on other sites