How to read an entire binary file all at once in C++

1 Answer

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

int main() 
{
	std::streampos size;
	char *databock;

	std::ifstream file("d:\\data.bin", std::ios::in | std::ios::binary | std::ios::ate);

	if (file.is_open())
	{
		size = file.tellg();
		databock = new char[size];

		file.seekg(0, std::ios::beg);
		file.read(databock, size);

		file.close();

		std::cout << databock << std::endl;

		delete[] databock;
	}
	else std::cout << "Error open file" << std::endl;

	return 0;
}
/*
run:

data.bin
--------
A text for binary file

*/

 



answered Nov 16, 2017 by avibootz

Related questions

1 answer 504 views
5 answers 455 views
1 answer 229 views
2 answers 347 views
2 answers 279 views
1 answer 180 views
1 answer 565 views
...