[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Reading Binay files in C/C++

Posted on 2009-02-14
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++ ?

Question by:bbk_doll
  • 3
  • 2
  • 2
  • +1
LVL 13

Expert Comment

ID: 23643265
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.
LVL 13

Expert Comment

ID: 23643391
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();

Author Comment

ID: 23644065
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 13

Expert Comment

ID: 23644676
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

Accepted Solution

masheik earned 375 total points
ID: 23648904
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


Expert Comment

ID: 23649554
let me know still if you have query?
LVL 39

Expert Comment

ID: 23652903
>>>> 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?

Author Comment

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


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question