Greetings All,
I have a binary file that I need to read into a data structure, then insert into a SQL Server database. There is no header in the file. The binary file holds 496 records at 64 bytes per record. I am new to C++, so please forgive my ineptness. My long and int values appear to be read correctly. My char values however are displayed as misc characters (ie, smiley faces, misc letters, slashes, dashes). How can I interpret these char values correctly. I know 160 points isn't much for this question, but its all this poor kid has left :'( .
I am currently reading it in as follows:
struct Battery {
int BT;
int OC;
char BCU_U;
long BCU_L;
int COA;
char PWM;
char FU1;
char POR;
char WDT;
int BV;
int OBV;
int BVS1;
int BVS2;
int BVS3;
int BVS4;
int BVS5;
int BVS6;
long ELSY2K;
long DC;
long PBOT;
long SBOT;
int UVFC;
char FU2;
long BOT;
char BS;//[1];
char FU3;//[8];
char CSUM;//[1];
};
int length;
char * buffer;
std::ifstream is(DATABASE_FILE, ios_base::in | ios_base::binary);
//ifstream is;
//is.open (DATABASE_FILE, ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
// read data as a block:
int icur = 0;
int iblock = 64;
int istruct = sizeof(Battery);
int recnum = 0;
// allocate memory:
buffer = new char [iblock];
Battery bat;
do
{
recnum = recnum + 1;
//Read into our data structure
//is.read(buffer, iblock);
is.read(reinterpret_cast<c
har *>(&bat),sizeof(Battery));
//cout.write(buffer, iblock);
cout<<"BAT TEMP:"<<bat.BT<<endl;
cout<<"OVER CURRENT:"<<bat.OC<<endl;
cout<<"BAT CURRENT UPPER:"<<bat.BCU_U<<endl;
cout<<"BAT CURRENT LOWER:"<<bat.BCU_L<<endl;
cout<<"OPAMP:"<<bat.COA<<e
ndl;
cout<<"PWM:"<<bat.PWM<<end
l;
cout<<"FU1:"<<bat.FU1<<end
l;
cout<<"POR:"<<bat.POR<<end
l;
cout<<"WDT:"<<bat.WDT<<end
l;
cout<<"BUS VOLT:"<<bat.BV<<endl;
cout<<"BATTERY VOLTAGE:"<<bat.OBV<<endl;
cout<<"STAVE1:"<<bat.BVS1<
<endl;
cout<<"STAVE2:"<<bat.BVS2<
<endl;
cout<<"STAVE3:"<<bat.BVS3<
<endl;
cout<<"STAVE4:"<<bat.BVS4<
<endl;
cout<<"STAVE5:"<<bat.BVS5<
<endl;
cout<<"STAVE6:"<<bat.BVS6<
<endl;
cout<<"TIME:"<<bat.ELSY2K<
<endl;
cout<<"DELTA CONSUMED:"<<bat.DC<<endl;
cout<<"PRI BAT ON:"<<bat.PBOT<<endl;
cout<<"SEC BAT ON:"<<bat.SBOT<<endl;
cout<<"UNDER VOLT FAULT:"<<bat.UVFC<<endl;
cout<<"FU2:"<<bat.FU2<<end
l;
cout<<"BAT OFF TIME:"<<bat.BOT<<endl;
cout<<"BAT STAT:"<<bat.BS<<endl;
cout<<"FU3:"<<bat.FU3<<end
l;
cout<<"CHECKSUM:"<<bat.CSU
M<<endl;
cout<<endl;
ibv = bat.BV + ibv;
cout << "\n[hit enter]";
cin.get();
//set the pointer to this location
icur = icur + iblock;
is.seekg (icur, ios::beg);
} while (icur<length);
is.close();
//cout.write (buffer,length);
//return 0;
// give the user a chance to look things over before quitting
cout << "Total Records: " << recnum<<endl;
cout << "Avg Bus Volt: " << ibv/recnum;
cout << "\n[hit enter]";
cin.get();
Any help you can give is appreciated.
Thanks,
CodeYankee