reading bytes of data from binary file
-
Greetings..
I have two files one is having binary data and other file has structure of the messagesI need to open the binary file and based on the data type of the message i need to read bytes of data from the mentioned offset and store that back into the message
this is how the file which has structure of the message look like
(eg: read 4 bytes of data from 0th offset that is 0-3 bytes from binary file and store that value in msg_1)
please somebody help me implementing thisThanks in advance
-
@kook said in reading bytes of data from binary file:
please somebody help me implementing this
Help with what exactly? What did you try so far? What is not clear?
Reading arbitrary binary data from a file can be done with https://doc.qt.io/qt-5/qfile.html
If you are the one who creates the binary files you should consider using https://doc.qt.io/qt-5/qdatastream.html -
@jsulm i forgot to mention what i have done so for
i am not creating the binary file it is already there i just read it
this is how i am reading itvoid MainWindow::on_actionOpen_triggered()
{
//opening file to read
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
QFile file(fileName);//validating if (!file.open(QIODevice::ReadOnly)) { QMessageBox::warning(this, "Warning", file.errorString()); } file.seek(16); // mentioning the offset QByteArray bytes = file.read(4); //how many bytes i want to read qDebug() << bytes.toHex();
}
what i am not understanding is how i will read that offset and data type from the message.txt ( i have to read these values and pass )
file and store the read value back to the message -
@kook said in reading bytes of data from binary file:
how i will read that offset and data type from the message.txt
https://doc.qt.io/qt-5/qtextstream.html
You will also need to parse the text file to get needed information.
In your case it seems to be rather simple:- Read next line
- Split the line at ':' (https://doc.qt.io/qt-5/qstring.html#split-5)
- Check first column ([0]): if it is "datatype" then you know that the line contains information about data type, column [2] will contain its name
- If first column does not contain "datatype" then it contains the type name of the message, message name is in second column ([1]), its offset in third ([2])
-
@jsulm i have implemented as per your suggestion please check if i am doing it in a correct way and if not i am open for suggestions to improve
void MainWindow::on_actionOpen_triggered()
{
//opening log file to read
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
QFile file(fileName);//validating if (!file.open(QIODevice::ReadOnly)) { QMessageBox::warning(this, "Warning", file.errorString()); } //opening msgdata file to read QFile file1("/home/documents/msg.txt"); if(!file1.open(QIODevice::ReadOnly)) { qDebug() << "file not opened"; } QTextStream in(&file1); QString line; int offset = 0; int no_bytes = 0; while (in.readLineInto(&line)) { QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts); if (list[0] == "uint32") { no_bytes = 4; } else if ( list[0] == "uint16" ) { no_bytes = 2; } offset = list[2].toInt(); file.seek(offset); QByteArray bytes = file.read(no_bytes); qDebug() << bytes.toHex(); list[1] = QString(bytes); qDebug() << list[1]; }
}
and this part
if (list[0] == "uint32") { no_bytes = 4; }
i don't think this should be like this since i have to compare for many datatypes
please suggest a proper way to achive this -
@kook said in reading bytes of data from binary file:
please suggest a proper way to achive this
Create a QMap<QString, int> and use it to get the number of bytes for each datatype without the need to have many if..else:
// In your MainWindow class add this in private section: QMap<QString, int> dataTypes; // In MainWindow constructor initialize dataTypes: dataTypes["uint32"] = 4; dataTypes["uint16"] = 2; ... // In void MainWindow::on_actionOpen_triggered() get the number of bytes like this: no_bytes = dataTypes[list[0]];
But this only makes a difference if you have many data types!
-
This post is deleted!