Don't know about your structure internals but I think your "char" data are just short integers (8 bits instead 32-bit int), that's why I have suggested you to "cast" to integers when printing/displaying.
Main Topics
Browse All TopicsGreetings 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
//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
cout<<"PWM:"<<bat.PWM<<end
cout<<"FU1:"<<bat.FU1<<end
cout<<"POR:"<<bat.POR<<end
cout<<"WDT:"<<bat.WDT<<end
cout<<"BUS VOLT:"<<bat.BV<<endl;
cout<<"BATTERY VOLTAGE:"<<bat.OBV<<endl;
cout<<"STAVE1:"<<bat.BVS1<
cout<<"STAVE2:"<<bat.BVS2<
cout<<"STAVE3:"<<bat.BVS3<
cout<<"STAVE4:"<<bat.BVS4<
cout<<"STAVE5:"<<bat.BVS5<
cout<<"STAVE6:"<<bat.BVS6<
cout<<"TIME:"<<bat.ELSY2K<
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
cout<<"BAT OFF TIME:"<<bat.BOT<<endl;
cout<<"BAT STAT:"<<bat.BS<<endl;
cout<<"FU3:"<<bat.FU3<<end
cout<<"CHECKSUM:"<<bat.CSU
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: jaime_olivaresPosted on 2004-07-21 at 07:11:32ID: 11602783
Your char values are correct, just a presentation matter:
cout<<"BAT CURRENT UPPER:"<<bat.BCU_U<<endl;
will be better:
cout<<"BAT CURRENT UPPER:"<< ((int)(bat.BCU_U))<<endl;
This will print your char value as an integer