[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

C++ - Reading Binary File Into Data Structure

Asked by CodeYankee in C++ Programming Language

Tags: binary, read, file, data

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<char *>(&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<<endl;
            cout<<"PWM:"<<bat.PWM<<endl;
            cout<<"FU1:"<<bat.FU1<<endl;
            cout<<"POR:"<<bat.POR<<endl;
            cout<<"WDT:"<<bat.WDT<<endl;
            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<<endl;
            cout<<"BAT OFF TIME:"<<bat.BOT<<endl;
            cout<<"BAT STAT:"<<bat.BS<<endl;
            cout<<"FU3:"<<bat.FU3<<endl;
            cout<<"CHECKSUM:"<<bat.CSUM<<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
[+][-]07/21/04 07:11 AM, ID: 11602783Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C++ Programming Language
Tags: binary, read, file, data
Sign Up Now!
Solution Provided By: jaime_olivares
Participating Experts: 3
Solution Grade: B
 
[+][-]07/21/04 07:13 AM, ID: 11602805Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/21/04 08:34 AM, ID: 11603852Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/22/04 02:32 AM, ID: 11610492Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/22/04 10:31 AM, ID: 11614540Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/22/04 11:29 AM, ID: 11615142Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/22/04 11:40 AM, ID: 11615267Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81