Saving data as a .txt file using a for loop
I am trying to store recorded values from sensors to a ".txt" file, I have narrowed the error to the for-loop where I write the values to the ".txt" file but I have no idea what the error is, please help
- getsamples: sends the values to be saved to datalogger
- datalogger saves the lead values to the txt file but crashes in the for loop
- ecgstorestructure is the structure that holds the sensor information and time, it indicates the format that variables are saved in
The code can successfully create the txt file but it crashes as soon as I try use the forloop to write the lead values into the txt file
getsamples.cpp
Code:
#include "getsamples.h"
#include <QVector>
#include <QPointF>
#include <QDebug>
#include <QFile>
#include <QSettings>
#include <QCoreApplication>
#include <QtMath>
#include "ecgstorestructure.h"
#include "getsamples.h"
#include "datalogger.h"
void getsamples::sample(double elapsed, DataStream& inputStream)
{
DataLogger logger;
// qDebug() << ("getsamples: sample") << endl;
//Frame Format:
// Elapsed (ms) | Lead 1 | Lead 2 | Lead 3 | Respiration | Lead off detection
// Lead off: 1 = leads disconnected, 0 = leads connected
QVector<double> frame;
frame.append (elapsed);
frame << ecg-> readFrame();
static double lead1, lead2, lead3, respiration;
lead1 +=frame.at(1);
lead2 +=frame.at(2);
lead3 +=frame.at(3);
respiration += frame.at(4);
inputStream.append(EcgStoreStructure(frame.at(1), frame.at(2), frame.at(3), frame.at(4), elapsed/1000));
logger.save(inputStream);
datalogger.cpp
Code:
#include "datalogger.h"
#include "settingssingleton.h"
#include <QDebug>
#include <iostream>
#include "ecgstorestructure.h"
#include <QFile>
#include <QTextStream>
void DataLogger::saveAsText(DataStream &input){
QString mFilename2
= SettingsSingleton
::instance().
getFileName()+".txt";
QFile outFile
(mFilename2
);
{
qDebug()<< "could not open file for writing";
return;
}
for (int i ; i<input.size();i++){
// ERROR OCCURS HERE
const EcgStoreStructure tmp=input.at(i);
out2 << tmp.toText() << endl; //"\n";
}
outFile.close();
ecgstorestructure.cpp
Code:
#include "ecgstorestructure.h"
EcgStoreStructure::EcgStoreStructure()
{
}
EcgStoreStructure::EcgStoreStructure(double _frame1, double _frame2, double _frame3, double _resp, double _time): frame1(_frame1), frame2(_frame2),frame3(_frame3),time(_time),resp(_resp){
}
QString EcgStoreStructure
::toText() const {
return tmp;
}
EcgStoreStructure::~EcgStoreStructure(){
}
datastream.cpp
Code:
#include "datastream.h"
DataStream::DataStream(){
}
DataStream::~DataStream(){
}
QString DataStream
::toText(int index
){ return this->at(index).toText();
}
Re: Saving data as a .txt file using a for loop
In the for() loop on line 22 of datalogger.cpp, you have not initialized the loop variable "i" to anything, so it has a random value. This is almost certainly outside of the range of your datastream object's content, so trying to retrieve the input from that location crashes.
Re: Saving data as a .txt file using a for loop
You are a hero, thanks a million