CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    98

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

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Printing out Bytes of a File

    Quote Originally Posted by ejohns85 View Post
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Printing out Bytes of a File

    Quote Originally Posted by ejohns85 View Post
    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

  4. #4
    Join Date
    Aug 2006
    Posts
    98

    Re: Printing out Bytes of a File

    Thanks Paul - using the "rb" argument worked!

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Printing out Bytes of a File

    Code:
    (int)pow(2.0, i);
    Try using this instead
    Code:
    1 << i;
    "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

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Printing out Bytes of a File

    Quote Originally Posted by ejohns85 View Post
    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

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    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
  •  





Click Here to Expand Forum to Full Width

Featured