Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Last 52 lines are not written in csv file.
Qt 6.11 is out! See what's new in the release blog

Last 52 lines are not written in csv file.

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dev_liya

    I have one csv file in which 3 column and 866300 lines. I have try to write this data into other csv file. when i try it has write 866248 lines in file after that remaining 52 lines are not write in file. what is the problem i do not understand it. i try to debug it using print on console then it has print till last on the console. only the problem in write in file.

    #include <QCoreApplication>
    #include <QFile>
    #include <QStringList>
    #include <QDebug>
    #include <QTextStream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QFile file("C:/Users/Liya Dev/Downloads/Patient22.csv");
    
        QFile write("new_data.csv");
    
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug()<<file.errorString();
            return 1;
        }
    
        if(!write.open(QIODevice::WriteOnly |QIODevice::Append))
        {
            qDebug()<<file.errorString();
            return 1;
        }
    
        QTextStream out(&write);
    
        QStringList data;
        while (!file.atEnd())
        {
            QString line = file.readLine();
            data = line.split(',');
            if (data[0]=="v1")
            {
                out<<line;
                continue;
            }
            else
            {
                int seq = (data[0].toInt())-1;
                QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
                qDebug()<<str;
                out<<str<<"\n";
            }
        }
        return a.exec();
    }
    
    

    please help.
    this is the console output.
    console output.PNG
    this is the csv file output.
    file output.PNG

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @dev_liya Your app is apparently crashing!
    Please run through debugger and post stack trace after crash.
    I guess you have out of bounds exception as you are accessing a list via index without any bounds checking!

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    D 1 Reply Last reply
    1
    • jsulmJ jsulm

      @dev_liya Your app is apparently crashing!
      Please run through debugger and post stack trace after crash.
      I guess you have out of bounds exception as you are accessing a list via index without any bounds checking!

      D Offline
      D Offline
      dev_liya
      wrote on last edited by
      #3

      @jsulm not crashed. working fine. i have stopped using stop button that's way it is showing unexpectedly finished line.

      jsulmJ 1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @dev_liya said in Last 52 lines are not written in csv file.:

        QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();

        You should check the size of data.

        Also running your program in the debugger will show you where exactly it is crashing.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • D dev_liya

          @jsulm not crashed. working fine. i have stopped using stop button that's way it is showing unexpectedly finished line.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @dev_liya Try to add write.flush() call before you call "return a.exec()" (after your loop).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          1
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Apart from all the stuff above - why do you need to covert , to ; - Excel and LibreOffice calc can import any csv file with a self-defined separator.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Apart from all the stuff above - why do you need to covert , to ; - Excel and LibreOffice calc can import any csv file with a self-defined separator.

              D Offline
              D Offline
              dev_liya
              wrote on last edited by
              #7

              @Christian-Ehrlicher What you said i cannot understand it.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @dev_liya Try to add write.flush() call before you call "return a.exec()" (after your loop).

                D Offline
                D Offline
                dev_liya
                wrote on last edited by
                #8

                @jsulm I have try it. the output is same nothing change.

                jsulmJ 1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  And your last line do not start with "v1" ?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  D 1 Reply Last reply
                  0
                  • D dev_liya

                    @jsulm I have try it. the output is same nothing change.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @dev_liya I also would close the file before calling "return a.exec()".

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    JonBJ 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      And your last line do not start with "v1" ?

                      D Offline
                      D Offline
                      dev_liya
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher only first row contain v1 after all rows contain data.

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • D dev_liya

                        @Christian-Ehrlicher only first row contain v1 after all rows contain data.

                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @dev_liya Then remove all rows except the ones which are not put into the new file and try again. If they still don't get into the new file, please post the content of those lines.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @dev_liya I also would close the file before calling "return a.exec()".

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #13

                          @jsulm said in Last 52 lines are not written in csv file.:

                          @dev_liya I also would close the file before calling "return a.exec()".

                          @dev_liya
                          You should definitely do this (correctly). Without closing (or flushing) the file, the last "block" will not have been sent to the file when you do your a.exec() --- it will still sit in memory. It seems likely this would be the cause of your "Last 52 lines are not written in csv file"?

                          1 Reply Last reply
                          1
                          • D Offline
                            D Offline
                            dev_liya
                            wrote on last edited by
                            #14

                            @jsulm @Christian-Ehrlicher @JonB

                            QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
                            

                            i have removed "trimmed()" from this line and also remove \n from this below line.

                            out<<str<<"\n";
                            

                            then it has been print till 866261. after that not print. what is the problem i cannot under stand.

                            JonBJ 1 Reply Last reply
                            0
                            • Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              Reduce your file to see if it's the length or the specific line as already suggested

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • D dev_liya

                                @jsulm @Christian-Ehrlicher @JonB

                                QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
                                

                                i have removed "trimmed()" from this line and also remove \n from this below line.

                                out<<str<<"\n";
                                

                                then it has been print till 866261. after that not print. what is the problem i cannot under stand.

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #16

                                @dev_liya said in Last 52 lines are not written in csv file.:

                                then it has been print till 866261. after that not print. what is the problem i cannot under stand.

                                Have you acted on closing or flushing the file as suggested, or ignored that?

                                This is a separate question from why you got or have "The program has unexpectedly finished", which you must resolve if it's still happening. If it happens while writing lines to the file, you will indeed find further lines are missing, you must not get such an error.

                                D 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @dev_liya said in Last 52 lines are not written in csv file.:

                                  then it has been print till 866261. after that not print. what is the problem i cannot under stand.

                                  Have you acted on closing or flushing the file as suggested, or ignored that?

                                  This is a separate question from why you got or have "The program has unexpectedly finished", which you must resolve if it's still happening. If it happens while writing lines to the file, you will indeed find further lines are missing, you must not get such an error.

                                  D Offline
                                  D Offline
                                  dev_liya
                                  wrote on last edited by dev_liya
                                  #17

                                  @JonB I have tried flush and close. but i am not getting required output but getting same output as mentioned above. i am not getting any error. my program is working fine other then this issue.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • D dev_liya

                                    @JonB I have tried flush and close. but i am not getting required output but getting same output as mentioned above. i am not getting any error. my program is working fine other then this issue.

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #18

                                    @dev_liya

                                    • You might show your code now so we can check the close().
                                    • Could you please state clearly whether or not you still get "Program has finished unexpectedly"?
                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      dev_liya
                                      wrote on last edited by
                                      #19

                                      @jsulm @Christian-Ehrlicher @JonB my issue is solved thank you all for your help and reply. i have write wrong object name with close(). now my issue was solved. thank you all. and sorry for my mistake.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved