4 Replies - 514 Views - Last Post: 27 October 2013 - 11:47 AM Rate Topic: -----

#1 me_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 03-May 12

Binary file Reading

Posted 19 October 2013 - 10:10 AM

Hi Everyone!

I have the file "timelog.dat", which is a binary file with measure data from different sensors Called A, B, C etc and Each measure data is stored as a structure of the following appearance

strcut DataRecord{ 
char logger; // Define logging Point A, B, C 
double temp; // logged Temperature 
time_t locTime; //Local Time
};



My program is reading this file printing all data to console from the sensor A and the rest of the data I am ignoring. which is almost achieved although I have couple of problems:

1. Rounding up temp(type double) to 1 decimal place
2. Displaying locTime format: "Sat Oct 10 15:50:15 2013"


Here is my Code: and so, for i am able to display it on the screen but not the way I want it.

#include <iostream>
#include <fstream>
#include <ctime>
#include <time.h>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <stdio.h>

using namespace std;

void readBinaryFile();

struct DataRecord{
    char logger;
    double temp;
    time_t locTime;
};

int main()
{
    DataRecord dRecord;
    readBinaryFile();
    return 0;
}

void readBinaryFile(){
    char logger;
    double temp;
    time_t locTime;

    fstream myReadFile;
    myReadFile.open("timelog.dat",ios::in | ios::binary);
    cout << "Data A" << endl;
    while(!myReadFile.eof()){
        myReadFile.read((char *)(&logger), sizeof(logger));
        if(logger == 'A'){
            myReadFile.read((char *)(&temp), sizeof(temp));
            cout << fixed << setprecision(1) << temp << ", " ;
            myReadFile.read((char *)(&locTime), sizeof(locTime));

            struct tm * timeinfo;
            time (&locTime);
            timeinfo = localtime((&locTime));
            printf("%s", asctime(timeinfo));
        }
    }
}



The problem with my code is:

1. Temperature is not rounded to 1 decimal place (some of them are converted to 0.0 and the rest to very long sequence of numbers
2. Time stamp is converted to the format which i need but it only displays the current system time not the time from my "timelog.dat" file.

any idea what mistake I am making?

I have attached the two pictures of the output...
1. Original values as they are stored in the timelog.dat file
2. screenshot... picture of converted values

And the format of output i want is:
2.4, Tue Oct 06 15:54:15 2009
.
.
and so on

Regards,

Ewa

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Binary file Reading

#2 me_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 03-May 12

Re: Binary file Reading

Posted 19 October 2013 - 11:08 AM

I solved the problem of Time stamp

char cstr_time[1024] ;
std::strftime( cstr_time, sizeof(cstr_time), "%a %b %d %H:%M:%S %Y\n", std::localtime(&timestamp)) ;
std::cout << cstr_time ;



but still the problem of rounding temperature to 1 decimal point is there :(
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Binary file Reading

Posted 19 October 2013 - 11:20 AM

Moved to C/C++. Please do not post help questions in the Advanced Discussion forum.
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Binary file Reading

Posted 19 October 2013 - 02:32 PM

Shouldn't you be reading an entire struct?
That seems like the logical thing to do if you have a struct denoting a data record. (I suspect your odd output might indicate that you are reading the incorrect bits.)

Here's how I would read/write binary files.
#include <iostream>
#include <fstream>
#include <ctime>
#include <time.h>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <stdio.h>

using namespace std;

void readBinaryFile();

struct DataRecord{
    char logger;
    double temp;
    time_t locTime;
};

void produceBinaryFile()
{
    ofstream out("timelog.dat", ios::binary);
    
    for(int i = 0; i < 5; i++)
    {
        time_t rawTime;
        time(&rawTime);
        DataRecord dr = {'A', (double)i, rawTime};
        out.write((char*)&dr, sizeof(DataRecord));
    }
}

int main()
{
    //produceBinaryFile();
    readBinaryFile();
}

void readBinaryFile(){
    char logger;
    double temp;
    time_t locTime;

    fstream myReadFile;
    myReadFile.open("timelog.dat",ios::in | ios::binary);
    
    DataRecord dr;
    while(myReadFile.read((char*)&dr, sizeof(DataRecord)))
    {
        cout << fixed << setprecision(1) << dr.temp << ", ";
        cout << ctime(&dr.locTime) << "\n";
    }
}

Was This Post Helpful? 1
  • +
  • -

#5 me_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 03-May 12

Re: Binary file Reading

Posted 27 October 2013 - 11:47 AM

View Posteker676, on 19 October 2013 - 02:32 PM, said:

Shouldn't you be reading an entire struct?
That seems like the logical thing to do if you have a struct denoting a data record. (I suspect your odd output might indicate that you are reading the incorrect bits.)

Here's how I would read/write binary files.
#include <iostream>
#include <fstream>
#include <ctime>
#include <time.h>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <stdio.h>

using namespace std;

void readBinaryFile();

struct DataRecord{
    char logger;
    double temp;
    time_t locTime;
};

void produceBinaryFile()
{
    ofstream out("timelog.dat", ios::binary);
    
    for(int i = 0; i < 5; i++)
    {
        time_t rawTime;
        time(&rawTime);
        DataRecord dr = {'A', (double)i, rawTime};
        out.write((char*)&dr, sizeof(DataRecord));
    }
}

int main()
{
    //produceBinaryFile();
    readBinaryFile();
}

void readBinaryFile(){
    char logger;
    double temp;
    time_t locTime;

    fstream myReadFile;
    myReadFile.open("timelog.dat",ios::in | ios::binary);
    
    DataRecord dr;
    while(myReadFile.read((char*)&dr, sizeof(DataRecord)))
    {
        cout << fixed << setprecision(1) << dr.temp << ", ";
        cout << ctime(&dr.locTime) << "\n";
    }
}


Thanks eker676!

your help have solved my problem :)

Thank you very much and have a wonderful life

Regards
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1