Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

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 231 views
1 answer 202 views
1 answer 213 views
1 answer 167 views
1 answer 205 views
1 answer 606 views
...