gpt4 book ai didi

c++ - 可执行文件在复制时以某种方式损坏

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:10 25 4
gpt4 key购买 nike

我正在使用Qt的QNetworkAccessManager从一个地方(目前是本地机器,但将来会是一个HTTP服务器)下载一个文件并暂时存储在TEMP文件中(linux ubuntu)。我发现的问题是文件(可执行文件)在此过程中被损坏:当我尝试将文件作为可执行文件运行时,它返回有问题的交叉编译的经典错误。

这很有趣,因为该文件是嵌入式 Linux 设备的可执行文件 - 我正在将可执行文件下载到我的 TEMP,以便稍后将其发送到设备。但是,当发生这种情况时(使用 FileZilla),会出现此错误消息:

./re8k_interface-tgt: line 1: syntax error: unexpected word (expecting ")")

现在我知道原始文件可以通过将其复制到设备并运行它来正常运行,所以我知道它与复制文件的过程有关,无论是在下载时还是在写入 QFile 对象时。以下是我目前的做法:

//Call to download
QUrl ulrTemp("//" + downloadUrls[downloadStep].arg(ui->sbID->text()));
ulrTemp.setScheme("file");

qDebug() << "Downloading from" << ulrTemp;

poReply = downloadNetworkManager->get(QNetworkRequest(ulrTemp));

connect(poReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(slotTransferProgress(qint64,qint64)));
connect(poReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(slotTransferError(QNetworkReply::NetworkError)));


//When finished
QByteArray downloadedData;
downloadedData = reply->readAll();

reply->deleteLater();
poReply->deleteLater();

static const QString tempFilePath = QDir::tempPath();

QFile file(tempFilePath + "/" + downloadNames[downloadStep]);

if (!file.open(QFile::WriteOnly | QFile::Truncate))
{
qDebug() << "Failure opening temp file to write: " << file.fileName();

return;
}

QDataStream stream(&file);

stream << downloadedData;

file.close();

P.s.: 我知道设置权限的必要性

复制的文件大小与原始文件完全匹配。那么,我看不到的问题在哪里?

最佳答案

当将字节数组写入QDataStream时,数组的长度也会被写入。

根本不使用数据流,直接使用QFile,或者更好的QTemporaryFile

下面的示例演示了如何利用 C++11 和 Qt 5 使其变得非常简单:

Writing to: /var/folders/yy/2tl/T/download-29543601.L91178
Wrote 55015 bytes.
Downloaded 55015 of -1 bytes
Wrote 7572 bytes.
Wrote 6686 bytes.
Wrote 5104 bytes.
Downloaded 74377 of 74377 bytes
Successfully wrote /var/folders/yy/2tl/T/download-29543601.L91178
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTemporaryFile>
#include <QUrl>
#include <QByteArray>
#include <QTextStream>
#include <QDebug>
#include <cstdio>

QTextStream out(stdout);
QTextStream in(stdin);

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QNetworkAccessManager mgr;

auto url = QUrl("http://stackoverflow.com/questions/29543601/"
"executable-getting-somehow-corrupted-when-being-copied");
auto reply = mgr.get(QNetworkRequest(url));

QTemporaryFile file;
if (!file.open()) {
qDebug() << "Can't open file for writing.";
return -1;
}
out << "Writing to: " << file.fileName() << endl;

QObject::connect(reply, &QNetworkReply::downloadProgress, [](qint64 rx, qint64 total) {
qDebug() << "Downloaded" << rx << "of" << total << "bytes";
});

QObject::connect(reply, &QIODevice::readyRead, [reply, &file]{
auto data = reply->readAll();
auto written = file.write(data);
if (data.size() != written) {
qDebug() << "Write failed, wrote" << written << "out of" << data.size() << "bytes.";
} else {
qDebug() << "Wrote " << written << "bytes.";
}
});

QObject::connect(reply, &QNetworkReply::finished, [reply, &file]{
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "The request was unsuccessful. Error:" << reply->error();
qApp->quit();
return;
}
if (file.flush()) {
out << "Successfully wrote " << file.fileName();
out << "\nPress Enter to remove the file and exit." << flush;
in.readLine();
} else {
qDebug() << "The file flush has failed";
}
qApp->quit();
});

return a.exec();
}

关于c++ - 可执行文件在复制时以某种方式损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29543601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com