Qt Beispiel: 2 Dateien vergleichen
Nachdem ich schon einige male 2 Dateien bytegenau vergleiche musste, habe ich eine kleine Applikation geschrieben die das anhand von beiden Dateien als Übergabe Parameter macht.
Beispiel Ausgabe wenn die Datei mit sich selbst verglichen wird:
.\BinaryCompare.exe "BinaryCompare.exe" "BinaryCompare.exe"
Read at "0"
Read at "2800"
Read at "5000"
Read at "7800"
Read at "a000"
File does match. Number of bytes read: "44544"
Folgendes kann in main.cpp einer Qt-Konsolenandwendung eingefügt werden:
#include <QDebug>
#include <QFile>
#include <QString>
#include <QIODevice>
int main(int argc, char *argv[])
{
QString sFile1 = "";
QString sFile2 = "";
if(argc > 1)
{
sFile1 = argv[1];
}
if(argc > 2)
{
sFile2 = argv[2];
}
QFile oFile1(sFile1);
QFile oFile2(sFile2);
if(oFile1.open(QIODevice::ReadOnly))
{
if(oFile2.open(QIODevice::ReadOnly))
{
qint64 uiRead1 = 0;
qint64 uiRead2 = 0;
qint64 uiPosition = 0;
char pCompare1[10240];
char pCompare2[10240];
while( ((uiRead1 = oFile1.read(pCompare1, 10240)) != 0) &&
((uiRead2 = oFile2.read(pCompare2, 10240)) != 0) &&
uiRead1 <= 10240 &&
uiRead2 <= 10240 &&
uiRead1 == uiRead2
)
{
qDebug() << "Read at " << QString::number(uiPosition, 16);
for(int i=0; i<uiRead2; i++)
{
if(pCompare1[i] != pCompare2[i])
{
qDebug() << "Failed at " << QString::number(uiPosition + i, 16);
return -1;
}
}
uiPosition += uiRead2;
}
if(uiRead1 != uiRead2)
qDebug() << "File does match. Number of bytes read: "
<< QString::number(uiPosition);
oFile2.close();
}
else
{
qDebug() << "Failed to open File2: " << sFile2;
return -1;
}
oFile1.close();
}
else
{
qDebug() << "Failed to open File1: " << sFile1;
return -1;
}
return 0;
}
Folgendes wäre eine passende .pro Datei:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target