Results 1 to 3 of 3

Thread: Saving data as a .txt file using a for loop

  1. #1
    Join Date
    Jan 2017
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,799
    Thanks
    264
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    VitaminG (23rd January 2017)

  4. #3
    Join Date
    Jan 2017
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Saving data as a .txt file using a for loop

    You are a hero, thanks a million

Similar Threads

  1. Saving two data sets to the same file
    By amirlevi in forum Newbie
    Replies: 3
    Last Post: 31st August 2016, 13:08
  2. Saving Model Data to XML
    By Jarrod in forum Newbie
    Replies: 2
    Last Post: 14th August 2016, 11:15
  3. Saving Data as XML
    By aIsmail in forum Newbie
    Replies: 1
    Last Post: 18th November 2015, 09:14
  4. Replies: 1
    Last Post: 22nd September 2010, 16:39
  5. Saving pure plot data to image file
    By Debilski in forum Qwt
    Replies: 4
    Last Post: 7th April 2009, 18:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.