diehardii
January 20th, 2004, 11:31 PM
Hi, I have a quick question on formatting.
I am writing a program that takes a buffer of type char* and writes it to a logfile via
logfile << (unsigned int*)bufffer1[i];
Unfortunately, the output is always in the format
0x000000E00x0000001F0x0000001E0x00000081
I would like to put it into the format
E0 1F 1E 81
but I can't figure out how to do it with ofstream. The code is below and kind of quick and dirty. I have thousands of these strings to look at, so if anyone has any idea I'd greatly appreciate it. Thanks for your time.
~Steve
// Compares a nonfunctional mpeg and the same file made functional
#include <fstream.h>
#include <iomanip>
const char * file1 = "test.mpeg";
const char * file2 = "testcorrected.mpeg";
int main ()
{
unsigned char *buffer1;
unsigned char *buffer2;
unsigned char *buffer3;
long size1;
long size2;
long tot_byteoffsets = 0;
ifstream file1 (file1, ios::in | ios::binary | ios::ate);
ifstream file2 (file2, ios::in|ios::binary|ios::ate);
size1 = file1.tellg();
size2 = file2.tellg();
file1.seekg (0, ios::beg);
file2.seekg(0, ios::beg);
buffer1 = new unsigned char [size1];
buffer2 = new unsigned char [size2];
buffer3 = new unsigned char [size1];
file1.read(buffer1, size1);
file2.read(buffer2, size2);
file1.close();
file2.close();
// Setup log file
ofstream logfile("logfile.txt");
logfile << "Starting log \n\n" ;
cout << "the complete file is in a buffer" << endl;
cout << "The file size is " << size1 << endl;
cout << "Starting compare" << endl;
//Main comparison loop
int i = 0;
int j = 0;
while (i < size1)
{
if(buffer1[i] == buffer2[j])
{
buffer3[j] = buffer1[i];
i++;
j++;
}
else
{
int bytechange = 0;
logfile << "The following string differs at byte offset " << i << endl;
while((buffer1[i] != buffer2[j]) || (buffer1[i+1] != buffer2[j+1]) ||
(buffer1[i+2]!= buffer2[j+2]))
{
bytechange++;
logfile << (unsigned int*) buffer1[i];
if (bytechange%4 == 0)
logfile << " ";
i++;
}
logfile << "\nTotal bytes - " << bytechange<< endl << endl;
//j++;
tot_byteoffsets += bytechange;
buffer3[j] = buffer1[i];
}
}
//write mpegfile based on comparison
ofstream myFile ("output.mpeg", ios::out | ios::binary);
myFile.write(buffer3,(size1-tot_byteoffsets));
cout << "Total kilobytes difference is " << (tot_byteoffsets/1024) << endl;
delete[] buffer1;
delete[] buffer2;
delete[] buffer3;
return 0;
}
I am writing a program that takes a buffer of type char* and writes it to a logfile via
logfile << (unsigned int*)bufffer1[i];
Unfortunately, the output is always in the format
0x000000E00x0000001F0x0000001E0x00000081
I would like to put it into the format
E0 1F 1E 81
but I can't figure out how to do it with ofstream. The code is below and kind of quick and dirty. I have thousands of these strings to look at, so if anyone has any idea I'd greatly appreciate it. Thanks for your time.
~Steve
// Compares a nonfunctional mpeg and the same file made functional
#include <fstream.h>
#include <iomanip>
const char * file1 = "test.mpeg";
const char * file2 = "testcorrected.mpeg";
int main ()
{
unsigned char *buffer1;
unsigned char *buffer2;
unsigned char *buffer3;
long size1;
long size2;
long tot_byteoffsets = 0;
ifstream file1 (file1, ios::in | ios::binary | ios::ate);
ifstream file2 (file2, ios::in|ios::binary|ios::ate);
size1 = file1.tellg();
size2 = file2.tellg();
file1.seekg (0, ios::beg);
file2.seekg(0, ios::beg);
buffer1 = new unsigned char [size1];
buffer2 = new unsigned char [size2];
buffer3 = new unsigned char [size1];
file1.read(buffer1, size1);
file2.read(buffer2, size2);
file1.close();
file2.close();
// Setup log file
ofstream logfile("logfile.txt");
logfile << "Starting log \n\n" ;
cout << "the complete file is in a buffer" << endl;
cout << "The file size is " << size1 << endl;
cout << "Starting compare" << endl;
//Main comparison loop
int i = 0;
int j = 0;
while (i < size1)
{
if(buffer1[i] == buffer2[j])
{
buffer3[j] = buffer1[i];
i++;
j++;
}
else
{
int bytechange = 0;
logfile << "The following string differs at byte offset " << i << endl;
while((buffer1[i] != buffer2[j]) || (buffer1[i+1] != buffer2[j+1]) ||
(buffer1[i+2]!= buffer2[j+2]))
{
bytechange++;
logfile << (unsigned int*) buffer1[i];
if (bytechange%4 == 0)
logfile << " ";
i++;
}
logfile << "\nTotal bytes - " << bytechange<< endl << endl;
//j++;
tot_byteoffsets += bytechange;
buffer3[j] = buffer1[i];
}
}
//write mpegfile based on comparison
ofstream myFile ("output.mpeg", ios::out | ios::binary);
myFile.write(buffer3,(size1-tot_byteoffsets));
cout << "Total kilobytes difference is " << (tot_byteoffsets/1024) << endl;
delete[] buffer1;
delete[] buffer2;
delete[] buffer3;
return 0;
}