Writing float value to file

I'm trying to write the value of xcord to the file, but it's come out as jiberish like š™ for some reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{

	LPCSTR fPath = "C:\\c++ test.txt";
	const	float xcord = 5.3;
	DWORD bytesToWrite;
	HANDLE	hFile = CreateFile(fPath,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,0);


	if(hFile == 0)
	{
		cout << "hFile error " << endl;
	}



	WriteFile(hFile,&xcord,2,&bytesToWrite,0);




	return 0;
}
Last edited on
1) IEEE 754 single precision floating point numbers are 32 bits or 4 bytes in size.
2)
but it's come out as jiberish like š™ for some reason.
If you opened file to see it, it should be like that, because binary representation is not intended to be represented in readable format.
If you write the value as binary you have to read it as binary as well in order to make sense of it. Reading the binary representation of a float as text will give you gibberish, as you said.
Last edited on
Topic archived. No new replies allowed.