当前位置: 动力学知识库 > 问答 > 编程问答 >

open/save to binary file in c++ fails intermittently

问题描述:

I've been staring at this to long...

I have made a program that logs weather data from different sensors. It handles the data in a double linkedlist and saves it to a binary file. To store different "compressions" of the data different files are used. e.g no compression, hours, days, etc.

The main program first loads the content of the correct file (defined by the WeatherSeries constructor) and adds all content from the file into the linkedlist. Then adds the new element and then saves it all. It adds to the list from oldest to newest and in the file it is also saved so the newest record is the last to be added to the file.

The error that occurs is that it seems that I lose a couple of hours of recorded data. I have observed that the data have existed. I.e. seen that there were data recorded between e.g. 9PM and 10PM and then in the morning after this data is gone.

The weird thing is the following:

  • Error only occurs intermittent and only for the barometric sensor that delivers values with 6 digits compared to the humidity and temperature sensors which delivers values with four digits.

  • It has only happened to the "no compression" and never for any of the other compressions. This means that the program that retrieves the data from the sensors works. Also it means that the function that adds data to the double linkedlist works.

Left is the functions that opens and saves the data to the files.

Can you please see if you can find any errors in my code?

 void weatherSeries::saveSeries()

{

ostringstream s;

s << "WLData/" << mSensorNbr << "_" << mSensorType << "_" << mTimeBase << ".dat";

ofstream file(s.str().c_str(), ios::out | ios::trunc | ios::binary);

if (!file)

{

file.clear();

file.open(s.str().c_str(), ios::out | ios::trunc | ios::binary);

}

if(file.is_open())

{

for (current = tail; current != NULL; current = current->prev)

{

file.write((char*)&current->time_stamp, sizeof(time_t));

file.write((char*)&current->val_avg, sizeof(double));

file.write((char*)&current->min, sizeof(double));

file.write((char*)&current->max, sizeof(double));

file.write((char*)&current->nbrOfValues, sizeof(unsigned long int));

}

}

else

{

cerr << "Unable to open for saving to " << mSensorNbr << "_" << mSensorType << "_" << mTimeBase << ".dat";

}

file.close();

}

void weatherSeries::openSeries()

{

deleteAll();

ostringstream s;

s << "WLData/" << mSensorNbr << "_" << mSensorType << "_" << mTimeBase << ".dat";

ifstream file(s.str().c_str(), ios::in | ios::binary);

if (!file)

{

file.clear();

file.open(s.str().c_str(), ios::in | ios::binary);

}

if(file.is_open())

{

time_t tmp_TS = 0;

double tmp_val_avg = 0;

double tmp_min = 0;

double tmp_max = 0;

unsigned long int tmp_nbrOfValues = 0;

while (file.read((char*)&tmp_TS, sizeof(time_t)))

{

file.read((char*)&tmp_val_avg, sizeof(double));

file.read((char*)&tmp_min, sizeof(double));

file.read((char*)&tmp_max, sizeof(double));

file.read((char*)&tmp_nbrOfValues, sizeof(unsigned long int));

addToSeries(tmp_TS, tmp_val_avg, tmp_min, tmp_max, tmp_nbrOfValues, true);

}

}

else

{

cerr << "Unable to open for opening from " << mSensorNbr << "_" << mSensorType << "_" << mTimeBase << ".dat";

}

file.close();

}

Note: deleteAll() clears the double linkedlist.

网友答案:

You were correct. The error was found in another part of the program. When I started logging different things in the code.

More or less different mechanism instantiate the program and it happened to happen at the same time causing the file to be manipulated by to instances at the same time.

分享给朋友:
您可能感兴趣的文章:
随机阅读: