Jump to content
  • Advertisement
Sign in to follow this  
GavinC

Strange problem writing/reading non-ascii version of file (bytes)

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have recently been working on a level-exporter for Maya and have run into an issue when outputting my information as a series of bytes and then trying to read them back in again. For each of the variables I convert them into the required number of unsigned char objects (each taking 1 byte, so four of these for an integer, four for a float, 8 for a double, 4 for a DWORD, and so on) and then output those to file. The problem is that some specific values appear to output an unreadable character, usually one that forces a new-line in the file. An example of this is any floating-point value where the first two decimal places are .57, which will output a character that forces a new-line, but not one that is actually read back in later (I describe how I'm reading the data further below). I'll provide source code to a simplified example of the problem, complete with output results (as this usually helps).
/*
Example program that demonstrates this.
(Simple console application, held entirely in main.cpp).
*/

#include <stdio.h>
#include <conio.h>

#include <fstream>
static std::ofstream o("Dump.log");

void convertFloatToBytes(float value)
{
	union
	{
		float aValue;
		unsigned char b[4];
	} in;

	in.aValue = value;

	o << "Converted value " << value << " has byte code of [" << in.b[0] << in.b[1] << in.b[2] << in.b[3] << "]" << std::endl;
}


int main()
{
	printf("Hello world\n");

	float someValue = -34.5734;
	float someCorrectValue = -46.5919;
	convertFloatToBytes(someValue);
	convertFloatToBytes(someCorrectValue);

	getch();
	return 0;
}




This program outputs the following results: (in dump.log, the output stream defined statically in the above code).
/*
Converted value -34.5734 has byte value of [)K
Â] //This cannot (third character is seemingly "missing" when data-stream is read in as one big "chunk").

Converted value -46.5919 has byte value of [^:Â] //This can be read in correctly
*/




Each value SHOULD have 4 bytes (as the second one, someCorrectValue does). However, the first value has third byte replaced with a newline, which causes problems when reading the data back in (as that byte is missing completely). ============================ How I'm reading the data in: (Please note: This works 100% correctly for all characters except that one character) ============================ Step1: Entire level file is read in as a data "chunk" and stored in virtual memory. I then assign a pointer of type "unsigned char*" (called 'data') to the first element in that data so that I can assign pointer-type variables to any section of that data, and it will cover the appropriate number of bytes (for example, if I assign an integer pointer to the first element, then it will automatically read-in elements 1->4 as that integer when I copy it's value). Step2: I keep track of the offset into 'data', and adjust the offset and read the values accordingly (assigning pointers of the correct type to that offset in data, and then copying it's values and storing them locally within the level-code). ======== Results: ======== This seems to work 100% correctly until the first instance of the "problem" character. After that, all values are out-of-place by one character, because the "problem" character isn't read in as a character and is therefore missing. Naturally, then, this throws-off the entire data stream from that point onwards. ----------------------------------------------------------- I'm confident that this problem has probably been run-into before, seeing as it's not something specific within my code (all I'm doing is dumping the data 1 byte at a time to file), so this problem has likely been addressed and solved many times before. I'd appreciate any help with this problem (as well as any other byte-values which might cause problems that I haven't run into yet), as it's the last hurdle before getting my level-format working. Thanks in advance for any help. ~Gavin Costello~

Share this post


Link to post
Share on other sites
Advertisement
Some weird operating systems (*krhm* Windows *krhm*) make a distinction between "binary" and "text" files - particularly, in the way newlines are handled. Open your file stream in binary mode:
static std::ofstream o("Dump.log", ios::binary);

Share this post


Link to post
Share on other sites
Wow, that was a fast reply. I'll check that solution as soon as I get home. If it's something as simple as that then... "d'oh". This thing has been holding up the completion of my level format for about 3 days now (I've tried some of the most far-out-there things to fix it, I didn't know that ofstream specified between ascii and binary though, so I'll try that).

Will post here again once I've checked this solution.

[EDIT:]
I tested it with a simple little application on a laptop here and it SEEMS to work (or at least it outputs all 4 bytes). Once I get home I can check it... but it looks more promising than the things I was trying...

*goes back to test it soon, will post again with results, thanks again*

[Edited by - GavinC on August 25, 2005 3:49:00 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!