The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Reading Bytes in binary file
Discuss Reading Bytes in binary file in the C Programming forum on Dev Shed. Reading Bytes in binary file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 4th, 2004, 08:36 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Reading Bytes in binary file
I am doing a project on C++...
I need to dump the content of a binary file to the screen, that is each byte in that file is being print out.
How can I do this?
Please help...
|

January 4th, 2004, 08:50 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
So dump it. I presume what you really mean is that you want to see a text representation of the binary data in the file, right? You can dump it in integer, hex, ones and zeros, even base 64 if you have a mind. You can print the hex representation by taking each character value in the binary file and doing a printf("%x", charVal).
|

January 4th, 2004, 09:04 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
What i mean is that...
if I use "od -t x1 test.bin"
the content is :
0000000 45 4c 01 02 73 72
Then the C++ project need to have the following output:
45 4c 01 02 73 72
I am using UNIX
|

January 4th, 2004, 09:12 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
It is printing in hex, us the printf statement I showed you.
|

January 4th, 2004, 09:14 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|

January 4th, 2004, 09:24 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
I am most familiar with C I/O, but this will work just as well in C++:
Code:
#include <stdio.h>
int main(){
FILE *fin;
int val, cnt=0;
if ((fin = fopen("somefile.txt", "rb")) == NULL){
fprintf(stderr, "Can't open file for reading!\n");
return 1;
}
while ((val = fgetc(fin)) != EOF){
printf("%02X ", (unsigned char) val);
cnt++;
if (cnt % 20 == 0)
printf("\n");
}
fclose(fin);
printf("\n");
return 0;
}
|

January 4th, 2004, 09:56 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks, i get it...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|