How to create .bin file?
-
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(); }
-
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(); }
@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...
-
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(); }
@Vijaykarthikeyan
In addition to @jsulm, in your existing code one cannot write to aqrc:
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 theQIODeviceBase::Text
flag. A.bin
file presumably will not want that. -
@Vijaykarthikeyan
In addition to @jsulm, in your existing code one cannot write to aqrc:
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 theQIODeviceBase::Text
flag. A.bin
file presumably will not want that.@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?
-
@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?
@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?
-
@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?
@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 -
@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?
@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. -
@Vijaykarthikeyan
In addition to @jsulm, in your existing code one cannot write to aqrc:
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 theQIODeviceBase::Text
flag. A.bin
file presumably will not want that.@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(); }
-
@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(); }
@Vijaykarthikeyan
I don't know whether there is a question here? You have not heeded the suggestion to useQStandardPaths
to generate the file paths, and you have ignored that you need theText
flag as you are under Windows so the files generated will not be "proper" for Windows. -
@Vijaykarthikeyan
I don't know whether there is a question here? You have not heeded the suggestion to useQStandardPaths
to generate the file paths, and you have ignored that you need theText
flag as you are under Windows so the files generated will not be "proper" for Windows. -
@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.@JonB Is that .xml created automatically in executable? Because,in exe folder, there are only file formats like .dat and .pak?
-
@JonB Is that .xml created automatically in executable? Because,in exe folder, there are only file formats like .dat and .pak?
@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". -
@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...
-
@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". -
@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...
-
@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...
@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
-
@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
@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? -
@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
@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 aqrc:
for this, so why are we even discussing such a file? -
@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?
@J-Hilk Thank you so much. Yes, it is possible through QStandardPath .
-
@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 aqrc:
for this, so why are we even discussing such a file?@JonB I understand why it can't be? Again thank you for suggesting another problem which is writing a file through QStandarPath.