为什么file.read没有返回正确的字节数?

时间:2015-03-09 22:11:52

标签: python c++ qt

我试图用C ++重写这个python函数

def hash(name):
    readsize = 64 * 1024
    with open(name, 'rb') as f:
        size = os.path.getsize(name)
        data = f.read(readsize)
        f.seek(-readsize, os.SEEK_END)
        data += f.read(readsize)
    return hashlib.md5(data).hexdigest()

这是我尝试过的。

void hash(QString name) {
    QFile file(name);
    long int readsize =  64 * 1024;  
    long int size = file.size();

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "coundn't open your file";
        return;
    }

    QByteArray data = file.read(readsize);
    file.seek(size - readsize);
    data += file.read(readsize);

    QString h = QString(QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex());
    return QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex();
}

令人惊讶的是,这个C ++函数只在readsize < 340-ish时有效。我不知道为什么,但是如果读取量更大,它甚至不会返回正确的字节数(我希望len(数据)== 4 *最后读取),第二个file.read不返回正确的字节数。

0 个答案:

没有答案
相关问题