Could anyone please tell me how to read data from a binary file???..

It depends on the contents of the file -- every binary file is different. In a nutshell, use ifstream's read() method.

It is a hexadecimal file and I have to read four bytes from the file.

Then you will not have to open that file as binary. If the file looks like this: 0x01 0x02 0x03 0x04 OR maybe even like this: 1B 1C 1D 1E 1F (numbers separated by white space) Then you read it like this:

ifstream in("something.txt");
int a, b, c, d;
in >> hex >> a >> b >> c >> d;

On the otherhand, if the file contains the binary value of an integer, then read it like this (assuming sizeof(long) = 4 on your computer and compiler)

long x;
ifstream in("something.txt", ios::binary);
in.read( (char*)&x, sizeof(long));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.