ifstream swf;
char *h=(char*)malloc(sizeof(char)*3);
swf.read(h,sizeof(char)*3);
binary files
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
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)
You also might want to make sure you''re opening it in binary too (which I hope you are :D)
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".
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".
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
My 2D game engine
Not sure if this is going to help you. I copied this from my TGA loader. This part reads the header of the TGA.
I am not sure about your code but i would think it would look something like this:
Hope it helps (and is correct code... )
[edited by - Android_s on January 6, 2004 10:31:30 AM]
unsigned char tgaHeader[6]; // Declare an array to store the header info inifstream 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 beginninginFile.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 bytesifstream 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 fileinFile.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]
I tried that stuff. It didnt work... Here''s my whole function.
In the flash file format doc it says
but I keep getting a bunch of garbage after the FWS.
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.
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''.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement