How to write (output) array of floats to a binary file in C++

1 Answer

0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;

int main()
{
	float arr[3] = { 3.14, 87.91, -12.85 };
	char filename[] = "d:\\data.bin";

	std::ofstream ofs(filename, std::ios::out | std::ios::binary);
	if (!ofs) {
		cout << "Error open file" << endl;
		return 1;
	}
	ofs.write((char *)&arr, sizeof(arr));
	ofs.close();

	cout << endl;

	return 0;
}

/*
data.bin
--------

ֳץH@לׁ¯Bš™Mֱ

*/

/*
run:

*/

 



answered Jun 6, 2018 by avibootz

Related questions

1 answer 204 views
1 answer 202 views
1 answer 291 views
1 answer 250 views
1 answer 235 views
1 answer 247 views
...