-
September 4th, 2012, 02:14 PM
#1
Printing out Bytes of a File
Hello,
I am encoding some information in a binary file, and I want to check what I am doing by printing out all the bytes that represent the file.
This is being done by opening a pointer to the file with fopen, reading in each byte of data as a char, and then writing this char to the screen.
I have some image files (e.g. "image.jpg"), whose structure I know, so that I can test my program.
When I print out the chars, they are initially correct, and follow the structure of the file as expected.
However, after about 40 bytes, I find that every subsequent character is ' ' i.e. a blank character.
I then created a CharToBin function, which allows me to print out the actual bits in the char. When doing this, it shows that all the bits are 1 for the characters. i.e. most of the file is represented by 1's, which is clearly not correct.
This happens on all the image files I have tested, and furthermore, on several other non-image files. They all start printing out ' ' after a while. However, all these files are fine and not corrupted, e.g. the image files display correctly.
Any ideas on what is going wrong? You should be able to run the code below - can anybody run it and see if they get similar results?
Thanks!
Code:
#include <fstream>
#include <iostream>
#include <sstring>
#include <string>
#include <stdio.h>
using namespace std;
string CharToBin(char ch)
{
bool bits[8];
for (int i = 0; i < 8; i++)
{
bits[i] = ch & (int)pow(2.0, i);
}
stringstream bin_stream;
for (int i = 7; i >=0; i--)
{
bin_stream << bits[i];
}
string bin = bin_stream.str();
return bin;
}
int main()
{
FILE *fp = fopen("image.jpg", "r");
char key = '0';
while (key != 'q')
{
char c = getc(fp);
cout << c << endl;
cout << CharToBin(c) << endl;
key = getchar();
}
fclose(fp);
return 0;
}
-
September 4th, 2012, 02:40 PM
#2
Re: Printing out Bytes of a File
Originally Posted by ejohns85
Hello,
I am encoding some information in a binary file, and I want to check what I am doing by printing out all the bytes that represent the file.
This is being done by opening a pointer to the file with fopen, reading in each byte of data as a char, and then writing this char to the screen.
I have some image files (e.g. "image.jpg"), whose structure I know, so that I can test my program.
When I print out the chars, they are initially correct, and follow the structure of the file as expected.
However, after about 40 bytes, I find that every subsequent character is ' ' i.e. a blank character.
How, exactly, are you "printing" these bytes? If you're printing actual characters (i.e. treating the data as if they were ASCII), then I'd say you're not viewing the data correctly.
Viggy
-
September 4th, 2012, 03:15 PM
#3
Re: Printing out Bytes of a File
Originally Posted by ejohns85
Hello,
I am encoding some information in a binary file, and I want to check what I am doing by printing out all the bytes that represent the file.
If you're reading a binary file, then you need to open it in binary mode:
Code:
FILE *fp = fopen("image.jpg", "rb");
Note that the mode is "rb", not "r". With "r", the runtime assumes that the file is a text file, thereby treating line feeds and carriage returns differently, as well as assuming that a certain character (ctrl-z for windows) is the EOF marker.
Regards,
Paul McKenzie
-
September 4th, 2012, 09:06 PM
#4
Re: Printing out Bytes of a File
Thanks Paul - using the "rb" argument worked!
-
September 7th, 2012, 03:18 AM
#5
Re: Printing out Bytes of a File
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
September 7th, 2012, 03:44 AM
#6
Re: Printing out Bytes of a File
Originally Posted by ejohns85
using the "rb" argument worked!
If you want to use C++ file streams (being as you're using C++ libraries already) then here's my take on your solution.
(I took the liberty of eliminating some of the tempories)
Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
string CharToBin(char ch)
{
stringstream bin_stream;
for (int i = 7; i >=0; --i)
{
bin_stream << ((ch & (1 << i)) != 0);
}
return bin_stream.str();;
}
int main()
{
ifstream file("image.jpg", ios::binary);
char key = '0';
char c;
while ((key != 'q') && (file >> c))
{
cout << static_cast<int>(c) << endl;
cout << CharToBin(c) << endl;
key = getchar();
}
return 0;
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
September 7th, 2012, 05:17 AM
#7
Re: Printing out Bytes of a File
... and if you want an hex output, you can use the boost hex algorithm:
Code:
#include <iostream>
#include <fstream>
#include <iterator>
#include <boost/algorithm/hex.hpp>
using namespace std;
using namespace boost::algorithm;
int main()
{
ifstream file("image.jpg", ios::binary);
hex( istream_iterator<char>( file ), istream_iterator<char>(), ostream_iterator<char>( cout ) );
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|