Reading Binay files in C/C++

Hello experts,
Please tell me what is meant by BInary file
how can i open a binary file and display its contents on screen in C/C++ ?

Who is Participating?
create a file binary_file.bin , and read that file using the following program,
#include <stdio.h>
#include <stdlib.h>
int main(void)
FILE *fp;
char byte; //A byte contains 8 bits
fp = fopen("binary_file.bin", "rb");
if (fp == NULL)
printf("Can't open %s\n", INPUT_FILE);
getchar ( );
exit(EXIT_FAILURE); /* exit if file does not available*/
while(!feof(fp)) //reads up to the end of the file
fread (&byte, 1, 1, fp);
printf("Hex value=%d\n",nyte);
printf("char value=%c\n",byte);
printf("Int value=%d\n",byte);
return 0;

Open in new window

A binary file is one that is not restricted to text data.  Each byte may contain any value between 0x00 and 0xff.  A text file follows some convention -- ASCII is one, Unicode is another -- where each byte contains (in the case of ASCII) or participates in representing (in the case of Unicode) a character.

For ASCII, the value 0x00 through 0x7f are defined. See
for further data.

I'll post an example in a moment.
Here's the example I mentioned.  Sorry about the delay.

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;      

void main() {
   // Must open the stream as binary
   ifstream ifs("c:\\startup.log", istream::in + istream::binary);
   // Let stringstream do the binary->ASCII conversion
   ostringstream ss;
   // Arbitrarily read 10 bytes
   for (int i = 0; i < 10; i++) {
      // Each byte gets converted and placed in the stringstream
      ss << "0x" << std::hex << std::setw(2) << ifs.get() << " ";
   cout << ss.str();
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

bbk_dollAuthor Commented:
Thanks josgood !

But, Please can you write the above code in C ?
i know initially i requested for C or C++, But, sorry, i need it in C only.
Yes, I can do that and I will be happy to work with you on this.

It looks to me as though this is a homework question, so I will provide some initial guidance and then we can work together to get a solution that is clear to you.

This FAQ page explains why:

Reading a binary file requires you to open the file in binary mode and read data from it.  Here's an example of opening a binary file and reading a single byte from it.  Try extending this example to displaying the entire file on the screen.  

   // gives detail on fopen
   FILE *fp = fopen("C:\\myfile.bin","rb");
   if (0 == fp) printf("Couldn't open the file\n");
   else {
      // This example reads a single byte
      unsigned char aByte = 0;
         size_t bytesRead = fread(&aByte, sizeof(unsigned char), 1, fp);
         // display the byte in hex

For an example of reading the entire file into memory at once, see
let me know still if you have query?
>>>> It looks to me as though this is a homework question,

@masheik: it would have been nice if you had supported josgood by not giving full code to a possible homework question. Didn't you read his comment or did you just don't care?
bbk_dollAuthor Commented:
Thanks all for your help.
It was not a homework. i was learning file concepts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.