Sign in to follow this  

Problems reading back values from a file

This topic is 679 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

Hi guys,

I am in the process of writing a program that will read back floating point values from a file.

The files I am reading back are 2D co-ordinates packed xyxyxy... etc..

I am opening and loading the entire file into a 'memblock'

		fs.open(filename, std::fstream::in, std::fstream::binary);

		if (fs.is_open())
			std::cout << "File opened ok\r\n";
		else
		{
			std::cout << "File not found!\r\n";
			system("PAUSE");
			return 1;
		}

		fs.seekg(0, ios::end);
		nSize = (int)fs.tellg();
		memblock = new char[nSize];
		fs.seekg(0, ios::beg);
		fs.read(memblock, nSize);
		fs.close();
Then I am reading the values back like this (for the moment, whilst debugging).

		for (int i = 0;i < nVertexCount;i++)
		{
			memcpy(&tempX, memblock + (8 * i), sizeof(float));
			std::cout << "" << tempX << "\t";

			memcpy(&tempY, memblock + 4 + (8 * i), sizeof(float));
			std::cout << "" << tempY << "\t";
		}
The problem that I am encountering is that a lot of the values being displayed to the screen are completely incorrect (eg 5.60519e-45), whilst the others are correct.

With my test files, I am expecting to see values between -2.0f and 2.0f.

I know the test files are ok as I am using them as the source of model data in another application.

I am suspecting the problem lies in the 'memblock' being the type of char instead of unsigned char, but the type of 'char' appears to be the only way I can make the file_open functions compile.

Any advice would be hugely appreciated smile.png

Share this post


Link to post
Share on other sites

The problem that I am encountering is that a lot of the values being displayed to the screen are completely incorrect (eg 5.60519e-45), whilst the others are correct.

 

 

Whether that value is incorrect or not only you can say but for all intents and purposes that is effectively 0. Consider the Planck length which is supposedly the smallest thing that can be and that's only of the order 1.6 x 10-35  m. If your values were meters then that value is over a billion times smaller than the Planck length. Just something to consider.

 

Of course if it should be 2 and you get that number then something is definitely wrong. 

 

It might be useful to see how these values are initially written to the file if that is possibly.

Share this post


Link to post
Share on other sites


		fs.open(filename, std::fstream::in, std::fstream::binary);
I'm not sure what function you are calling there since I could not find any documentation of a three argument open member function, but you want
fs.open(filename, std::fstream::in | std::fstream::binary);
Note the operator | instead of the ','. Also, while this does not matter, I would usually prefer to call the flags std::ios::in and std::ios::binary since they belong to streams in general and not just file streams.

Genius, changing the comma to a pipe fixed it instantly.

It is odd because I copy and pasted the code from the project that I made that does the encoding in the first place.

I was obviously lucky that the writing component worked in the first place - hehe.

Thanks again guys! Saved my day :)

Share this post


Link to post
Share on other sites
Why do you think, that the values are incorrect ? Eg 5.60519e-45 is between -2.0f and 2.0f. Try to output them in a formated way, something like printf("%.2f,%.2f,%2.f",...).

Share this post


Link to post
Share on other sites

Why do you think, that the values are incorrect ?
Eg 5.60519e-45 is between -2.0f and 2.0f. Try to output them in a formated way, something like printf("%.2f,%.2f,%2.f",...).


Because I created the input file.

Couple that with the fact that replacing comma with the pipe as suggested by BitMaster, bought back the values that I was anticipating to see.

Share this post


Link to post
Share on other sites

This topic is 679 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this