How to read a binary files and print 3's on screen?
Hello, I want to read a binary file of integer type and print the occurrence of the number of 3's in the file. I somehow wrote a program to open and read a binary file. Here is the couple of problems I am facing: 1. If I try to print the file on my terminal, the execution continues forever and the loop never ends. 2. I have no idea of how to filter out 3's from it. Here is my code: #include <iostream> #include <fstream> using namespace std; int main () { streampos size; char * memblock; ifstream file ("threesData.bin", ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); cout << "the entire file content is in memory"; for (int i = 0; i < size; i += sizeof(int)) { cout << *(int*)&memblock[i] << endl; } delete[] memblock; } else cout << "Unable to open file"; return 0; }