如何从 QFile 获取字节数?

发布于 2024-12-02 02:42:20 字数 516 浏览 2 评论 0原文

我有这个代码:

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九八野马 2024-12-09 02:42:20

根据 Qt 文档

qint64 QFile::size () const [虚拟]

从 QIODevice::size() 重新实现。

返回文件的大小。

对于 Unix 上的常规空文件(例如 /proc 中的空文件),此函数返回 0;此类文件的内容是根据您调用 read() 按需生成的。

它确实返回字节数,而不是字符数(我假设这就是您所说的“符号”的意思。请注意,size() 返回 qint64,而不是 int。

如果您使用 另外,

为什么要使用 int 指针?这样做没有任何好处,只需在堆栈上创建它:

qint64 size1 = 0;

qint64 size2 = 0;

According to Qt's docs:

qint64 QFile::size () const [virtual]

Reimplemented from QIODevice::size().

Returns the size of the file.

For regular empty files on Unix (e.g. those in /proc), this function returns 0; the contents of such a file are generated on demand in response to you calling read().

It does return the number of bytes, not the number of characters (I'm assuming that's what you mean by "symbols". Note that size() returns a qint64, not an int.

Your code should work as expected if you use a qint64.

Also, why are you using pointers for int? There is no benefit in doing that, just create it on the stack:

qint64 size1 = 0;

qint64 size2 = 0;

柏林苍穹下 2024-12-09 02:42:20

请记住,当您将一个字符写入文件时,无论如何在大多数情况下它可能都是一个字节。

#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();
}

稍微修改了你的代码。然后会产生:

Returning true 
"Size 1 is: 5" 
"Size 2 is: 12"

看看文件大小与字符数如何匹配?每个字符都是一个字节,这就是为什么它看起来像是对字符进行计数的。

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.

苦笑流年记忆 2024-12-09 02:42:20

另外 -- 比较

if (size1 < size2)
{
    return true;
}

不会做你想的那样,它比较 ptr 地址。你可能想要...

if (*size1 < *size2)
{
    return true;
}

但正如 Lucky Luke 所说,没有理由将它们放在堆上。

Also -- comparing

if (size1 < size2)
{
    return true;
}

Is not going to do what you think, it compares ptr addresses. You probably want...

if (*size1 < *size2)
{
    return true;
}

But as Lucky Luke said, there's no reason to put these on the heap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文