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

x
?
Solved

Reading Binay files in C/C++

Posted on 2009-02-14
8
Medium Priority
?
307 Views
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++ ?

Thanks.
0
Comment
Question by:bbk_doll
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Expert Comment

by:josgood
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
   http://www.asciitable.com/
for further data.

I'll post an example in a moment.
0
 
LVL 13

Expert Comment

by:josgood
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() << " ";
   }
   ifs.close();
   cout << ss.str();
}
0
 

Author Comment

by:bbk_doll
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.
Please.
0
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

by:josgood
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:
http://www.experts-exchange.com/help.jsp

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
         printf("%02x",aByte);
      }

For an example of reading the entire file into memory at once, see
http://www.cplusplus.com/reference/clibrary/cstdio/fread.html
0
 
LVL 9

Accepted Solution

by:
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);
}
 
fclose(fp);
return 0;
}

Open in new window

0
 
LVL 9

Expert Comment

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

Expert Comment

by:itsmeandnobodyelse
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?
0
 

Author Comment

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

Thanks.
0

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