How to read complete binary file in buffer with C++

2 Answers

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

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

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

	if (file.is_open()) {
		int pos = file.tellg();
		char *buf = new char[pos + 1];
		file.seekg(0, std::ios::beg);
		file.read(buf, pos);
		file.close();

		buf[pos] = NULL;

		cout << buf << endl;

		delete[] buf;
	}
	else
		cout << "Error open file" << endl;

	return 0;
}


/*
run:

├⌡H@∞╤»BתשM┴

*/

 



answered Jun 7, 2018 by avibootz
edited Jun 7, 2018 by avibootz
0 votes
#include <iostream>
#include <fstream>
#include <vector>

using std::cout;
using std::endl;
using std::ifstream;
using std::vector;

int main()
{
	ifstream file("d:\\data.bin", std::ios::binary | std::ios::ate);
	std::streamsize size = file.tellg();
	file.seekg(0, std::ios::beg);

	vector<char> buffer(size);
	if (file.read(buffer.data(), size)) {
		for (char ch : buffer)
			cout << ch << " ";
	}

	return 0;
}


/*
run:

├ ⌡ H @ ∞ ╤ » B ת ש M ┴ 

*/

 



answered Jun 7, 2018 by avibootz

Related questions

1 answer 245 views
1 answer 218 views
1 answer 234 views
1 answer 183 views
1 answer 226 views
1 answer 631 views
...