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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.0k Views 1 Watching
  • 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 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

    jsulmJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      1
      • T Tyler_QQ

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        3
        • mrjjM mrjj

          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 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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            • jsulmJ jsulm

              @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 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

              mrjjM 1 Reply Last reply
              0
              • T Tyler_QQ

                @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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Tyler_QQ
                Super. Please mark as solved.

                1 Reply Last reply
                1

                • Login

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