We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Reading Binay files in C/C++

bbk_doll asked
Medium Priority
Last Modified: 2012-06-21
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++ ?

Watch Question

Top Expert 2007

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.
Top Expert 2007

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();


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.
Top Expert 2007

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.  

   // http://www.cprogramming.com/tutorial/cfileio.html 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
Software Engineer
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
masheikSoftware Engineer

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?


Thanks all for your help.
It was not a homework. i was learning file concepts.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.