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

Qt Code:
  1. #include "getsamples.h"
  2. #include <QVector>
  3. #include <QPointF>
  4. #include <QDebug>
  5. #include <QFile>
  6. #include <QSettings>
  7. #include <QCoreApplication>
  8. #include <QtMath>
  9. #include "ecgstorestructure.h"
  10. #include "getsamples.h"
  11. #include "datalogger.h"
  12.  
  13. void getsamples::sample(double elapsed, DataStream& inputStream)
  14. {
  15.  
  16. DataLogger logger;
  17.  
  18. // qDebug() << ("getsamples: sample") << endl;
  19. //Frame Format:
  20. // Elapsed (ms) | Lead 1 | Lead 2 | Lead 3 | Respiration | Lead off detection
  21. // Lead off: 1 = leads disconnected, 0 = leads connected
  22.  
  23. QVector<double> frame;
  24. frame.append (elapsed);
  25. frame << ecg-> readFrame();
  26. static double lead1, lead2, lead3, respiration;
  27.  
  28. lead1 +=frame.at(1);
  29. lead2 +=frame.at(2);
  30. lead3 +=frame.at(3);
  31. respiration += frame.at(4);
  32.  
  33. inputStream.append(EcgStoreStructure(frame.at(1), frame.at(2), frame.at(3), frame.at(4), elapsed/1000));
  34. logger.save(inputStream);
To copy to clipboard, switch view to plain text mode 

datalogger.cpp
Qt Code:
  1. #include "datalogger.h"
  2. #include "settingssingleton.h"
  3. #include <QDebug>
  4. #include <iostream>
  5. #include "ecgstorestructure.h"
  6. #include <QFile>
  7. #include <QTextStream>
  8.  
  9. void DataLogger::saveAsText(DataStream &input){
  10.  
  11. QString mFilename2 = SettingsSingleton::instance().getFileName()+".txt";
  12. QFile outFile(mFilename2);
  13.  
  14.  
  15. if(!outFile.open(QFile::WriteOnly | QFile::Text))
  16. {
  17. qDebug()<< "could not open file for writing";
  18. return;
  19. }
  20.  
  21. QTextStream out2(&outFile);
  22. for (int i ; i<input.size();i++){
  23. // ERROR OCCURS HERE
  24. const EcgStoreStructure tmp=input.at(i);
  25. out2 << tmp.toText() << endl; //"\n";
  26.  
  27.  
  28. }
  29.  
  30. outFile.close();
To copy to clipboard, switch view to plain text mode 

ecgstorestructure.cpp

Qt Code:
  1. #include "ecgstorestructure.h"
  2.  
  3. EcgStoreStructure::EcgStoreStructure()
  4. {
  5.  
  6. }
  7.  
  8. EcgStoreStructure::EcgStoreStructure(double _frame1, double _frame2, double _frame3, double _resp, double _time): frame1(_frame1), frame2(_frame2),frame3(_frame3),time(_time),resp(_resp){
  9.  
  10. }
  11.  
  12. QString EcgStoreStructure::toText() const {
  13. QString tmp = QString::number(time) + "\t" + QString::number(frame1) + "\t"
  14. + QString::number(frame2) + "\t" + QString::number(frame3) + "\t"
  15. + QString::number(resp);
  16.  
  17. return tmp;
  18.  
  19. }
  20.  
  21. EcgStoreStructure::~EcgStoreStructure(){
  22.  
  23. }
To copy to clipboard, switch view to plain text mode 

datastream.cpp
Qt Code:
  1. #include "datastream.h"
  2.  
  3. DataStream::DataStream(){
  4.  
  5. }
  6.  
  7. DataStream::~DataStream(){
  8.  
  9. }
  10.  
  11. QString DataStream::toText(int index){
  12. return this->at(index).toText();
  13. }
To copy to clipboard, switch view to plain text mode