Reading Binay files in C/C++

Posted on 2009-02-14
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
    LVL 13

    Expert Comment

    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

    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

    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.
    LVL 13

    Expert Comment

    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
    LVL 9

    Accepted Solution

    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

    LVL 9

    Expert Comment

    let me know still if you have query?
    LVL 39

    Expert Comment

    >>>> 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

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


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    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…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now