I have this code:
int *size1 = new int();
int *size2 = new int();
QFile* file = new QFile("C:/Temp/tf.txt");
file->open(QFile::WriteOnly | QFile::Truncate);
file->write(str);
*size1 = file->size();
file->close();
file->open(QFile::WriteOnly | QFile::Truncate);
file->write(strC);
*size2 = file->size();
file->close();
delete file;
if (size1 < size2)
{
return true;
}
else
{
return false;
}
delete size1;
delete size2;
I want to compare bytes in file. But it comparing number of symbols in file.
Keep in mind that when you write a character to a file it's probably going to be a byte, for the most part anyway.
#include <QtCore/QCoreApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char *str = "Hello";
char *strC = "Hello again!";
qint64 size1;
qint64 size2;
QFile* file = new QFile("/home/nick/tf.txt");
file->open(QFile::WriteOnly | QFile::Truncate);
file->write(str);
size1 = file->size();
file->close();
file->open(QFile::WriteOnly | QFile::Truncate);
file->write(strC);
size2 = file->size();
file->close();
delete file;
QString num1 = QString::number(size1);
QString num2 = QString::number(size2);
if (size1 < size2)
{
qDebug() << "Returning true";
qDebug() << "Size 1 is: " + num1;
qDebug() << "Size 2 is: " + num2;
return true;
}
else
{
return false;
}
return a.exec();
}
Modified your code slightly. This then produces:
Returning true
"Size 1 is: 5"
"Size 2 is: 12"
See how the file size matches the number of characters? Each character is a byte, that's why it looks like it's counted the characters.