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. How to write buffer's value into .csv file?

How to write buffer's value into .csv file?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.9k 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.
  • T Offline
    T Offline
    Tyler_QQ
    wrote on 21 Nov 2016, 07:53 last edited by Tyler_QQ
    #1

    Hi all,

    I have the buffer and wanted to write the buffer's value in the .csv file.

    Here is my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWidget>
    #include <QProcess>
    #include <QThread>
    #include <QList>
    #include <QStringList>
    #include <QDir>
    #include <QDebug>
    #include <QString>
    #include <QFileDialog>
    #include <QMessageBox>
    #include <QFile>
    #include <stdio.h>
    #include "unistd.h"
    
    .......
    
    QString filename = "/home/tyler/test2/Qtdata.csv";
    QFile file( filename );
    if( file.open(QFile::ReadWrite)){
       QTextStream out(&filename);
       for(int i = 0; i < datanum; i++){
             QString tmp = QString("%1,%2,%3\n").arg(b_data[0][i]).arg(b_data[1][i]).arg(b_data[2][i]);
             out << tmp;
               }              
      }
      file.close();
    

    The Qtdata.csv always get nothing .

    Could anyone give me some advice.

    Thank you!

    Best regards,

    Tyler_QQ

    J 1 Reply Last reply 21 Nov 2016, 09:03
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 21 Nov 2016, 07:58 last edited by mrjj
      #2

      Hi
      Do you get absolutely
      nothing in the file or just black blank lines?

      You should check the value of datanum
      qDebug() << "lines:" << datanum;

      T 1 Reply Last reply 22 Nov 2016, 08:03
      1
      • T Tyler_QQ
        21 Nov 2016, 07:53

        Hi all,

        I have the buffer and wanted to write the buffer's value in the .csv file.

        Here is my code:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QWidget>
        #include <QProcess>
        #include <QThread>
        #include <QList>
        #include <QStringList>
        #include <QDir>
        #include <QDebug>
        #include <QString>
        #include <QFileDialog>
        #include <QMessageBox>
        #include <QFile>
        #include <stdio.h>
        #include "unistd.h"
        
        .......
        
        QString filename = "/home/tyler/test2/Qtdata.csv";
        QFile file( filename );
        if( file.open(QFile::ReadWrite)){
           QTextStream out(&filename);
           for(int i = 0; i < datanum; i++){
                 QString tmp = QString("%1,%2,%3\n").arg(b_data[0][i]).arg(b_data[1][i]).arg(b_data[2][i]);
                 out << tmp;
                   }              
          }
          file.close();
        

        The Qtdata.csv always get nothing .

        Could anyone give me some advice.

        Thank you!

        Best regards,

        Tyler_QQ

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 21 Nov 2016, 09:03 last edited by
        #3

        @Tyler_QQ said in How to write buffer's value in to .csv file?:

        QTextStream out(&filename);

        should be

        QTextStream out(&file);
        

        You're writing into the string containing the file name!

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

        T 1 Reply Last reply 22 Nov 2016, 08:09
        3
        • M mrjj
          21 Nov 2016, 07:58

          Hi
          Do you get absolutely
          nothing in the file or just black blank lines?

          You should check the value of datanum
          qDebug() << "lines:" << datanum;

          T Offline
          T Offline
          Tyler_QQ
          wrote on 22 Nov 2016, 08:03 last edited by
          #4

          @mrjj

          Hi mrjj,

          Thanks for your reply.

          There is just blank line in my .csv file.

          The value of datanum and tmp's three value are correct.
          0_1479801703448_2016-11-22_160103.jpg

          Again thanks for your advice.

          Best Regards,

          Tyler_QQ

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 22 Nov 2016, 08:07 last edited by mrjj
            #5

            And @jsulm sharp eye caught the real bug.
            You are giving it the filename and not file :)
            file and not filename should be used as he mentions.
            So you write nothing to the file but all to your string :)

            QTextStream out(&filename);

            1 Reply Last reply
            0
            • J jsulm
              21 Nov 2016, 09:03

              @Tyler_QQ said in How to write buffer's value in to .csv file?:

              QTextStream out(&filename);

              should be

              QTextStream out(&file);
              

              You're writing into the string containing the file name!

              T Offline
              T Offline
              Tyler_QQ
              wrote on 22 Nov 2016, 08:09 last edited by
              #6

              @jsulm

              Hi jsulm,

              Thanks a lot!!

              Through your advice, I had fixed this error.

              0_1479802113294_2016-11-22_160818.jpg

              Best Regards,

              Tyler_QQ

              M 1 Reply Last reply 22 Nov 2016, 08:10
              0
              • T Tyler_QQ
                22 Nov 2016, 08:09

                @jsulm

                Hi jsulm,

                Thanks a lot!!

                Through your advice, I had fixed this error.

                0_1479802113294_2016-11-22_160818.jpg

                Best Regards,

                Tyler_QQ

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 22 Nov 2016, 08:10 last edited by
                #7

                @Tyler_QQ
                Super. Please mark as solved.

                1 Reply Last reply
                1

                4/7

                22 Nov 2016, 08:03

                • Login

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