如何从 QFile 获取字节数?
我有这个代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Qt 文档:
它确实返回字节数,而不是字符数(我假设这就是您所说的“符号”的意思。请注意,size() 返回 qint64,而不是 int。
如果您使用 另外,
为什么要使用 int 指针?这样做没有任何好处,只需在堆栈上创建它:
According to Qt's docs:
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:
请记住,当您将一个字符写入文件时,无论如何在大多数情况下它可能都是一个字节。
稍微修改了你的代码。然后会产生:
看看文件大小与字符数如何匹配?每个字符都是一个字节,这就是为什么它看起来像是对字符进行计数的。
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.
Modified your code slightly. This then produces:
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.
另外 -- 比较
不会做你想的那样,它比较 ptr 地址。你可能想要...
但正如 Lucky Luke 所说,没有理由将它们放在堆上。
Also -- comparing
Is not going to do what you think, it compares ptr addresses. You probably want...
But as Lucky Luke said, there's no reason to put these on the heap.