Reading Binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UnknownBlue
    New Member
    • Jan 2007
    • 14

    Reading Binary

    Hi.

    I am trying to read .dat files which is in binary using visual C++ 6.0. I want to display the first 100 char (in hex). But instead some weird char are being display. I opened the file through hex editor and this is what i am supposed to get: 2EFACCF69FFC84. ..

    Can anyone help me in this? Thanks in advance!
    Code:
    char *memblock;
    ifstream::pos_type size;
    ifstream file;
    
    int main ()
    {
    	int size, i;
    	char n;
    
    	file.open ("s0455_re.dat", ios::in|ios::binary|ios::ate);
    
    	if (file.is_open())
    	{
    		cout << "Able to open file!";	
                                   // Able to get the size since it is at the end
    		size = (int) file.tellg(); 
    	
    		// allocate memory
    		memblock = new char [size]; 
    		
    		file.seekg (0, ios::beg);
    		
    		for (i=0; i<100; i++)
    		{	
    			file.seekg (0, ios::cur);
    			file.read (memblock, 1);
    			n = *memblock;
    			cout << n;
    		}
    		
    		file.close();
    	}
    	else cout << "Unable to open file";
    	return 0;
    }
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by UnknownBlue
    Hi.

    I am trying to read .dat files which is in binary using visual C++ 6.0. I want to display the first 100 char (in hex). But instead some weird char are being display. I opened the file through hex editor and this is what i am supposed to get: 2EFACCF69FFC84. ..

    Can anyone help me in this? Thanks in advance!

    char *memblock;
    ifstream::pos_t ype size;
    ifstream file;

    int main ()
    {
    int size, i;
    char n;

    file.open ("s0455_re.dat" , ios::in|ios::bi nary|ios::ate);

    if (file.is_open() )
    {
    cout << "Able to open file!";
    // Able to get the size since it is at the end
    size = (int) file.tellg();

    // allocate memory
    memblock = new char [size];

    file.seekg (0, ios::beg);

    for (i=0; i<100; i++)
    {
    file.seekg (0, ios::cur);
    file.read (memblock, 1);
    n = *memblock;
    cout << n;
    }

    file.close();
    }
    else cout << "Unable to open file";
    return 0;
    }
    Hi. If you compare your code to this you may discover how to make it work:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      int length;
      char * buffer;
    
      ifstream is;
      is.open ("test.txt", ios::binary );
    
      // get length of file:
      is.seekg (0, ios::end);
      length = is.tellg();
      is.seekg (0, ios::beg);
    
      // allocate memory:
      buffer = new char [length];
    
      // read data as a block:
      is.read (buffer,length);
    
      is.close();
    
      cout.write (buffer,length);
    
      return 0;
    }

    Comment

    • UnknownBlue
      New Member
      • Jan 2007
      • 14

      #3
      Hi.

      Thanks for replying.
      I have tried that but it gives an output of two lines like =.
      Its not in terms of hex.

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by UnknownBlue
        Hi.

        Thanks for replying.
        I have tried that but it gives an output of two lines like =.
        Its not in terms of hex.
        Adjusted
        Code:
        #include <iostream>
        #include <fstream>
        using namespace std;
        
        int main () {
          int length;
          char * buffer;
          char output[2];
          int lp;
        
          ifstream is;
          is.open ("test.txt", ios::binary );
        
          // get length of file:
          is.seekg (0, ios::end);
          length = is.tellg();
          is.seekg (0, ios::beg);
        
          // allocate memory:
          buffer = new char [length];
        
          // read data as a block:
          is.read (buffer,length);
        
          is.close();
        
          if (length > 100) length = 100;
        
          for (lp = 0; lp < length; lp++) {
            sprintf(output, "%X", atoi(buffer[lp]));
            cout << output;
          }
          return 0;
        }

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          That last part will not work.
          this piece of code will print out in hex so maybe you can adapt it
          Code:
          char ar[30] = {0};
          char buffer[20] = {0};
          sprintf(ar, "%d", 123456789);
          sprintf(buffer, "%X", atol(ar));
          cout << buffer;
          If you read the file 4 chars at a time into a long variable you could achieve what you need rather than into a char array.

          Comment

          • UnknownBlue
            New Member
            • Jan 2007
            • 14

            #6
            It sort of work!
            Thanks!

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              with a few minor modifications your original program would work, e.g. in particular cast the byte read as a char to an int and use the io manipulator to print it as hex
              Code:
              #include <iostream>
              #include <fstream>
              using namespace std;
              
              char memblock;  // ** remove *
              ifstream::pos_type size;
              ifstream file;
              
              int main ()
              {
              int size, i;
              char n;
              
              file.open ("s0455_re.dat", ios::in|ios::binary|ios::ate);
              
              if (file.is_open())
              {
              cout << "Able to open file!";
              // Able to get the size since it is at the end
              size = (int) file.tellg();
              
              // allocate memory
              //memblock = new char [size];  // ** not needed
              
              file.seekg (0, ios::beg);
              
              for (i=0; i<100; i++)
              {
              file.seekg (0, ios::cur);
              file.read (&memblock, 1);  // ** added &
              //n = *memblock;  // ** remove
              cout << hex << (int) memblock;  // ** output as hex
              }
              
              file.close();
              }
              else cout << "Unable to open file";
              cin.get();
              return 0;
              }

              Comment

              • LSB
                New Member
                • Jan 2007
                • 8

                #8
                You sure may want to make some minor changes:

                Code:
                int size100;
                ……………..
                size100 = (size < 100) ? size : 100;
                ……………..
                for (i =0; i<size100; i++)
                ..................

                Comment

                Working...