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 create .bin file?
Forum Update on Monday, May 27th 2025

How to create .bin file?

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 4 Posters 8.5k 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.
  • V Offline
    V Offline
    Vijaykarthikeyan
    wrote on 25 Jul 2023, 06:15 last edited by Vijaykarthikeyan
    #1

    I want to create .bin format file to write into it when each time I run the software. I have implemented QFile for .txt and .csv format. But, Now I wish to do the same in .bin to write into it. Below is the code snippet for writing into the .csv and.txt file format(assume that all the variables/functions are pre declared/defined :

    void MainWindow::writeToLogFile()
    {
        QFile File("qrc:/resources/other files/logfile.txt");
        QFile File2("qrc:/resources/other files/logfile.csv");
    
        if(File.open(QIODevice::Append | QIODevice::ReadWrite))
        {
                                QTextStream stream(&File);
                                stream<<"Hai!!!Hello World";
        }
        if(File2.open(QIODevice::Truncate | QIODevice::ReadWrite))
        {
                                QTextStream stream2(&File2);
                                stream2<<"Hello World";
        }
        File.close();
        File2.close();
    }
    
    J J 2 Replies Last reply 25 Jul 2023, 06:32
    0
    • V Vijaykarthikeyan
      25 Jul 2023, 06:15

      I want to create .bin format file to write into it when each time I run the software. I have implemented QFile for .txt and .csv format. But, Now I wish to do the same in .bin to write into it. Below is the code snippet for writing into the .csv and.txt file format(assume that all the variables/functions are pre declared/defined :

      void MainWindow::writeToLogFile()
      {
          QFile File("qrc:/resources/other files/logfile.txt");
          QFile File2("qrc:/resources/other files/logfile.csv");
      
          if(File.open(QIODevice::Append | QIODevice::ReadWrite))
          {
                                  QTextStream stream(&File);
                                  stream<<"Hai!!!Hello World";
          }
          if(File2.open(QIODevice::Truncate | QIODevice::ReadWrite))
          {
                                  QTextStream stream2(&File2);
                                  stream2<<"Hello World";
          }
          File.close();
          File2.close();
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Jul 2023, 06:32 last edited by
      #2

      @Vijaykarthikeyan said in How to create .bin file?:

      .bin format file

      Do you want to write binary data? .bin does not say anything about in what format you want to store data. Please explain what the format should be...

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

      1 Reply Last reply
      3
      • V Vijaykarthikeyan
        25 Jul 2023, 06:15

        I want to create .bin format file to write into it when each time I run the software. I have implemented QFile for .txt and .csv format. But, Now I wish to do the same in .bin to write into it. Below is the code snippet for writing into the .csv and.txt file format(assume that all the variables/functions are pre declared/defined :

        void MainWindow::writeToLogFile()
        {
            QFile File("qrc:/resources/other files/logfile.txt");
            QFile File2("qrc:/resources/other files/logfile.csv");
        
            if(File.open(QIODevice::Append | QIODevice::ReadWrite))
            {
                                    QTextStream stream(&File);
                                    stream<<"Hai!!!Hello World";
            }
            if(File2.open(QIODevice::Truncate | QIODevice::ReadWrite))
            {
                                    QTextStream stream2(&File2);
                                    stream2<<"Hello World";
            }
            File.close();
            File2.close();
        }
        
        J Offline
        J Offline
        JonB
        wrote on 25 Jul 2023, 06:38 last edited by
        #3

        @Vijaykarthikeyan
        In addition to @jsulm, in your existing code one cannot write to a qrc: resource file --- they are read-only, for obvious reasons. A "log file" will need to be external.

        And if you are under Windows any .txt or .csv files need to be opened with the QIODeviceBase::Text flag. A .bin file presumably will not want that.

        V 2 Replies Last reply 25 Jul 2023, 06:45
        4
        • J JonB
          25 Jul 2023, 06:38

          @Vijaykarthikeyan
          In addition to @jsulm, in your existing code one cannot write to a qrc: resource file --- they are read-only, for obvious reasons. A "log file" will need to be external.

          And if you are under Windows any .txt or .csv files need to be opened with the QIODeviceBase::Text flag. A .bin file presumably will not want that.

          V Offline
          V Offline
          Vijaykarthikeyan
          wrote on 25 Jul 2023, 06:45 last edited by
          #4

          @JonB Yes.you are correct. Writing into other directory it works. I wonder Why?

          Now, If I want to run my software in any other target system, it is not sure that those systems have same directory which I have coded in my file? What is the solution?

          J J J 3 Replies Last reply 25 Jul 2023, 06:47
          0
          • V Vijaykarthikeyan
            25 Jul 2023, 06:45

            @JonB Yes.you are correct. Writing into other directory it works. I wonder Why?

            Now, If I want to run my software in any other target system, it is not sure that those systems have same directory which I have coded in my file? What is the solution?

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 25 Jul 2023, 06:47 last edited by J.Hilk
            #5

            @Vijaykarthikeyan said in How to create .bin file?:

            @JonB Yes.you are correct. Writing into other directory it works. I wonder Why?

            @JonB said in How to create .bin file?:

            they are read-only

            @Vijaykarthikeyan said in How to create .bin file?:

            Now, If I want to run my software in any other target system, it is not sure that those systems have same directory which I have coded in my file? What is the solution?

            https://doc.qt.io/qt-6/qstandardpaths.html


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            V 1 Reply Last reply 25 Jul 2023, 08:42
            1
            • V Vijaykarthikeyan
              25 Jul 2023, 06:45

              @JonB Yes.you are correct. Writing into other directory it works. I wonder Why?

              Now, If I want to run my software in any other target system, it is not sure that those systems have same directory which I have coded in my file? What is the solution?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Jul 2023, 06:49 last edited by
              #6

              @Vijaykarthikeyan said in How to create .bin file?:

              I wonder Why?

              qrc file content is built into the executable - so, you cannot write into it.
              "it is not sure that those systems have same directory which I have coded in my file?" - solution is to use folders which the platform where your app is running provides for application data. See https://doc.qt.io/qt-6/qstandardpaths.html + QStandardPaths::DocumentsLocation or QStandardPaths::AppDataLocation

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

              1 Reply Last reply
              0
              • V Vijaykarthikeyan
                25 Jul 2023, 06:45

                @JonB Yes.you are correct. Writing into other directory it works. I wonder Why?

                Now, If I want to run my software in any other target system, it is not sure that those systems have same directory which I have coded in my file? What is the solution?

                J Offline
                J Offline
                JonB
                wrote on 25 Jul 2023, 06:49 last edited by JonB
                #7

                @Vijaykarthikeyan said in How to create .bin file?:

                I wonder Why?

                Have you read up what Qt resource files are and where they are stored? They are embedded into your executable. They can be read at runtime, but you cannot write to them!

                A log file will need to be a real, external file in your file system. You can use QStandardPaths to help generate a path to a suitable directory, in a cross-platform manner.

                V 1 Reply Last reply 25 Jul 2023, 07:24
                0
                • J JonB
                  25 Jul 2023, 06:38

                  @Vijaykarthikeyan
                  In addition to @jsulm, in your existing code one cannot write to a qrc: resource file --- they are read-only, for obvious reasons. A "log file" will need to be external.

                  And if you are under Windows any .txt or .csv files need to be opened with the QIODeviceBase::Text flag. A .bin file presumably will not want that.

                  V Offline
                  V Offline
                  Vijaykarthikeyan
                  wrote on 25 Jul 2023, 06:55 last edited by
                  #8

                  @JonB I want to save the data which is coming from and writing via UDP. It's a string format data separated by comma. it should be running continously, with the help of Timer. Below is the complete code. Ignore the declaration and definition. I have declared/defined all the members and functions in header and other files.

                  #include "ui_mainwindow.h"
                  #include "mainwindow.h"
                  #include <QDataStream>
                  #include <QTimer>
                  #include <QDebug>
                  #include <QElapsedTimer>
                  #include <QFile>
                  #include <QDateTime>
                  #include <QFile>
                  using namespace std;
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                              writeToLogFile();
                              UDP_DATA="Some comma separated datas";
                      send_socket = new QUdpSocket();
                      arduino_available=true;
                      QTimer* timer = new QTimer(this);
                      connect(timer, &QTimer::timeout, this, &MainWindow::updateCommunication);
                      // Start the timer
                      timer->start(1);
                          }
                  
                  
                  
                  void MainWindow::updateCommunication()
                                      {
                                          sendDatagram();
                                          processResponse();
                                      }
                  
                  void MainWindow::sendDatagram()
                                      {
                                          QHostAddress receiverAddress("some ip address");
                  
                                          if(UDP_DATA==UDP_DATA2)
                                          {
                                              send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                                              qDebug()<<UDP_DATA;
                                              date = QDateTime::currentDateTime();
                                              formattedTime = date.toString("dd.MM.yyyy,hh:mm:ss,");
                                              formattedTimeMsg = formattedTime.toLocal8Bit();
                                              writeToLogFile();
                                          }
                                          else
                                          {
                  
                                          }
                  }
                  
                  void MainWindow::processResponse()
                                      {
                                          if (send_socket->state() == QUdpSocket::BoundState)
                                          {
                                          while (send_socket->hasPendingDatagrams())
                                              {
                                                  QHostAddress senderAddress;
                                                  quint16 senderPort;
                                                  datagram.resize(send_socket->pendingDatagramSize());
                                                  send_socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                                                  emit data_received(datagram);
                                                  qDebug()<<datagram;
                                              }
                                          }
                  
                                      }
                  
                  void MainWindow::set_data(QByteArray to_data)
                                      {
                                              UDP_DATA=to_data;
                                              UDP_DATA2=to_data;
                                      }
                  
                  
                  QByteArray MainWindow::get_datagram()
                                          {
                                          return datagram;
                  }
                  
                  
                  void MainWindow::writeToLogFile()
                  {
                      QFile File("file:///C:/new_soft_log_files/logfile.txt");
                      QFile File2("file:///C:/new_soft_log_files/logfile.csv");
                  
                      if(File.open(QIODevice::Truncate | QIODevice::ReadWrite))
                      {
                                              QTextStream stream(&File);
                  
                                              stream<<"Date,Time,OUTPUT,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,AHRS INPUT\n";
                                              stream<<formattedTime;
                                              stream<<datagram+"\t";
                                              stream<<UDP_DATA+"\n";
                      }
                      if(File2.open(QIODevice::Truncate | QIODevice::ReadWrite))
                      {
                                              QTextStream stream2(&File2);
                                              stream2<<"Date,Time,OUTPUT,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,AHRS INPUT\n";
                                              stream2<<formattedTime;
                                              stream2<<datagram+"\t";
                                              stream2<<UDP_DATA+"\n";
                      }
                      File.close();
                      File2.close();
                  }
                  
                  J 1 Reply Last reply 25 Jul 2023, 07:01
                  0
                  • V Vijaykarthikeyan
                    25 Jul 2023, 06:55

                    @JonB I want to save the data which is coming from and writing via UDP. It's a string format data separated by comma. it should be running continously, with the help of Timer. Below is the complete code. Ignore the declaration and definition. I have declared/defined all the members and functions in header and other files.

                    #include "ui_mainwindow.h"
                    #include "mainwindow.h"
                    #include <QDataStream>
                    #include <QTimer>
                    #include <QDebug>
                    #include <QElapsedTimer>
                    #include <QFile>
                    #include <QDateTime>
                    #include <QFile>
                    using namespace std;
                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                            {
                                ui->setupUi(this);
                                writeToLogFile();
                                UDP_DATA="Some comma separated datas";
                        send_socket = new QUdpSocket();
                        arduino_available=true;
                        QTimer* timer = new QTimer(this);
                        connect(timer, &QTimer::timeout, this, &MainWindow::updateCommunication);
                        // Start the timer
                        timer->start(1);
                            }
                    
                    
                    
                    void MainWindow::updateCommunication()
                                        {
                                            sendDatagram();
                                            processResponse();
                                        }
                    
                    void MainWindow::sendDatagram()
                                        {
                                            QHostAddress receiverAddress("some ip address");
                    
                                            if(UDP_DATA==UDP_DATA2)
                                            {
                                                send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
                                                qDebug()<<UDP_DATA;
                                                date = QDateTime::currentDateTime();
                                                formattedTime = date.toString("dd.MM.yyyy,hh:mm:ss,");
                                                formattedTimeMsg = formattedTime.toLocal8Bit();
                                                writeToLogFile();
                                            }
                                            else
                                            {
                    
                                            }
                    }
                    
                    void MainWindow::processResponse()
                                        {
                                            if (send_socket->state() == QUdpSocket::BoundState)
                                            {
                                            while (send_socket->hasPendingDatagrams())
                                                {
                                                    QHostAddress senderAddress;
                                                    quint16 senderPort;
                                                    datagram.resize(send_socket->pendingDatagramSize());
                                                    send_socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
                                                    emit data_received(datagram);
                                                    qDebug()<<datagram;
                                                }
                                            }
                    
                                        }
                    
                    void MainWindow::set_data(QByteArray to_data)
                                        {
                                                UDP_DATA=to_data;
                                                UDP_DATA2=to_data;
                                        }
                    
                    
                    QByteArray MainWindow::get_datagram()
                                            {
                                            return datagram;
                    }
                    
                    
                    void MainWindow::writeToLogFile()
                    {
                        QFile File("file:///C:/new_soft_log_files/logfile.txt");
                        QFile File2("file:///C:/new_soft_log_files/logfile.csv");
                    
                        if(File.open(QIODevice::Truncate | QIODevice::ReadWrite))
                        {
                                                QTextStream stream(&File);
                    
                                                stream<<"Date,Time,OUTPUT,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,AHRS INPUT\n";
                                                stream<<formattedTime;
                                                stream<<datagram+"\t";
                                                stream<<UDP_DATA+"\n";
                        }
                        if(File2.open(QIODevice::Truncate | QIODevice::ReadWrite))
                        {
                                                QTextStream stream2(&File2);
                                                stream2<<"Date,Time,OUTPUT,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,\t,AHRS INPUT\n";
                                                stream2<<formattedTime;
                                                stream2<<datagram+"\t";
                                                stream2<<UDP_DATA+"\n";
                        }
                        File.close();
                        File2.close();
                    }
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 25 Jul 2023, 07:01 last edited by
                    #9

                    @Vijaykarthikeyan
                    I don't know whether there is a question here? You have not heeded the suggestion to use QStandardPaths to generate the file paths, and you have ignored that you need the Text flag as you are under Windows so the files generated will not be "proper" for Windows.

                    V 1 Reply Last reply 25 Jul 2023, 07:08
                    0
                    • J JonB
                      25 Jul 2023, 07:01

                      @Vijaykarthikeyan
                      I don't know whether there is a question here? You have not heeded the suggestion to use QStandardPaths to generate the file paths, and you have ignored that you need the Text flag as you are under Windows so the files generated will not be "proper" for Windows.

                      V Offline
                      V Offline
                      Vijaykarthikeyan
                      wrote on 25 Jul 2023, 07:08 last edited by
                      #10

                      @JonB No..just know I've gone through the Qstandard page. For the question about the data format which @jsulm asked, I've replied with the full code. need to respect his time and words. that's why.. And thank you for your suggestion that qrc is read only and directed me to go for QStandardPath.

                      J 1 Reply Last reply 25 Jul 2023, 07:41
                      0
                      • J JonB
                        25 Jul 2023, 06:49

                        @Vijaykarthikeyan said in How to create .bin file?:

                        I wonder Why?

                        Have you read up what Qt resource files are and where they are stored? They are embedded into your executable. They can be read at runtime, but you cannot write to them!

                        A log file will need to be a real, external file in your file system. You can use QStandardPaths to help generate a path to a suitable directory, in a cross-platform manner.

                        V Offline
                        V Offline
                        Vijaykarthikeyan
                        wrote on 25 Jul 2023, 07:24 last edited by
                        #11

                        @JonB Is that .xml created automatically in executable? Because,in exe folder, there are only file formats like .dat and .pak?

                        J 1 Reply Last reply 25 Jul 2023, 07:34
                        0
                        • V Vijaykarthikeyan
                          25 Jul 2023, 07:24

                          @JonB Is that .xml created automatically in executable? Because,in exe folder, there are only file formats like .dat and .pak?

                          J Offline
                          J Offline
                          JonB
                          wrote on 25 Jul 2023, 07:34 last edited by JonB
                          #12

                          @Vijaykarthikeyan
                          What .xml file? There is no mention of any in your code. Nor do I know what .dat or .pak files are doing in your "exe folder".

                          V 1 Reply Last reply 25 Jul 2023, 07:42
                          0
                          • V Vijaykarthikeyan
                            25 Jul 2023, 07:08

                            @JonB No..just know I've gone through the Qstandard page. For the question about the data format which @jsulm asked, I've replied with the full code. need to respect his time and words. that's why.. And thank you for your suggestion that qrc is read only and directed me to go for QStandardPath.

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 25 Jul 2023, 07:41 last edited by
                            #13

                            @Vijaykarthikeyan said in How to create .bin file?:

                            I've replied with the full code

                            It would be good if you would answer the questions. Also, in the code you posted I don't see anything related to writing .bin file, so no idea how that should help us...

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

                            V 1 Reply Last reply 25 Jul 2023, 07:52
                            1
                            • J JonB
                              25 Jul 2023, 07:34

                              @Vijaykarthikeyan
                              What .xml file? There is no mention of any in your code. Nor do I know what .dat or .pak files are doing in your "exe folder".

                              V Offline
                              V Offline
                              Vijaykarthikeyan
                              wrote on 25 Jul 2023, 07:42 last edited by
                              #14

                              @JonB You have said to look-through Qt Resource System.In that they have mentioned .qrc file which is the .xml format where all the resource files located. That's why I have asked about it.

                              J 1 Reply Last reply 25 Jul 2023, 07:44
                              0
                              • V Vijaykarthikeyan
                                25 Jul 2023, 07:42

                                @JonB You have said to look-through Qt Resource System.In that they have mentioned .qrc file which is the .xml format where all the resource files located. That's why I have asked about it.

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 25 Jul 2023, 07:44 last edited by
                                #15

                                @Vijaykarthikeyan said in How to create .bin file?:

                                Qt Resource System.In that they have mentioned .qrc file which is the .xml format where all the resource files located.

                                The resource file itself is in XML format. But there will be no such file in the build folder. Resource files are built INTO the executable (I already mentioned that in this thread). So, don't know why you expect to see any .xml files in build folder...

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

                                1 Reply Last reply
                                1
                                • J jsulm
                                  25 Jul 2023, 07:41

                                  @Vijaykarthikeyan said in How to create .bin file?:

                                  I've replied with the full code

                                  It would be good if you would answer the questions. Also, in the code you posted I don't see anything related to writing .bin file, so no idea how that should help us...

                                  V Offline
                                  V Offline
                                  Vijaykarthikeyan
                                  wrote on 25 Jul 2023, 07:52 last edited by
                                  #16

                                  @jsulm That's where I'm getting struck. I don't know how to create it. Then,you have asked what format. I have mentioned that it's a string format

                                  J J 2 Replies Last reply 25 Jul 2023, 07:53
                                  0
                                  • V Vijaykarthikeyan
                                    25 Jul 2023, 07:52

                                    @jsulm That's where I'm getting struck. I don't know how to create it. Then,you have asked what format. I have mentioned that it's a string format

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 25 Jul 2023, 07:53 last edited by
                                    #17

                                    @Vijaykarthikeyan said in How to create .bin file?:

                                    I have mentioned that it's a string format

                                    So, basically a text file?
                                    Then what exactly is the problem?

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

                                    V 1 Reply Last reply 25 Jul 2023, 08:55
                                    0
                                    • V Vijaykarthikeyan
                                      25 Jul 2023, 07:52

                                      @jsulm That's where I'm getting struck. I don't know how to create it. Then,you have asked what format. I have mentioned that it's a string format

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 25 Jul 2023, 07:58 last edited by
                                      #18

                                      @Vijaykarthikeyan
                                      It is hard to understand what your question or issue is. But again a reminder: if you are wanting to write to a log file at runtime you won't be able to use a qrc: for this, so why are we even discussing such a file?

                                      V 2 Replies Last reply 25 Jul 2023, 08:47
                                      1
                                      • J J.Hilk
                                        25 Jul 2023, 06:47

                                        @Vijaykarthikeyan said in How to create .bin file?:

                                        @JonB Yes.you are correct. Writing into other directory it works. I wonder Why?

                                        @JonB said in How to create .bin file?:

                                        they are read-only

                                        @Vijaykarthikeyan said in How to create .bin file?:

                                        Now, If I want to run my software in any other target system, it is not sure that those systems have same directory which I have coded in my file? What is the solution?

                                        https://doc.qt.io/qt-6/qstandardpaths.html

                                        V Offline
                                        V Offline
                                        Vijaykarthikeyan
                                        wrote on 25 Jul 2023, 08:42 last edited by
                                        #19

                                        @J-Hilk Thank you so much. Yes, it is possible through QStandardPath .

                                        1 Reply Last reply
                                        1
                                        • J JonB
                                          25 Jul 2023, 07:58

                                          @Vijaykarthikeyan
                                          It is hard to understand what your question or issue is. But again a reminder: if you are wanting to write to a log file at runtime you won't be able to use a qrc: for this, so why are we even discussing such a file?

                                          V Offline
                                          V Offline
                                          Vijaykarthikeyan
                                          wrote on 25 Jul 2023, 08:47 last edited by
                                          #20

                                          @JonB I understand why it can't be? Again thank you for suggesting another problem which is writing a file through QStandarPath.

                                          1 Reply Last reply
                                          0

                                          3/22

                                          25 Jul 2023, 06:38

                                          topic:navigator.unread, 19
                                          • Login

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