3

假设我已经执行了 aQNetworkRequest并获得了适当的QNetworkReply. 如果它是一个大文件(比如 1 GB),我如何创建一个 4k 字节数组缓冲区并将 4k x 4k 的数据读取到该数组中并同时将其写入打开的文件流?例如,等效的 C# 代码是这样的(我熟悉 C# 而不是 Qt):

public static void CopyStream(Stream input, Stream output)
{
    // input is web stream, output is filestream
    byte[] buffer = new byte[4096];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write (buffer, 0, read);
    }
}

- - 编辑

实际上,我正在尝试做的是在每次运行我的应用程序时下载恢复功能。每次我尝试恢复下载时,我都会设置range标题QNetworkRequest并获取其余数据,因此不需要立即写入数据,而是逐步写入数据。

4

1 回答 1

1

You probably want a solution in c++ as Qt is a library. One possibility is to use QIODevice class, which is inherited by both QFile and QNetworkReply

void copyStream(QIODevice& input, QIODevice& output){
  std::vector<char> buffer(4096);
  qint64 bytesRead;
  while ((bytesRead=input.read(&buffer[0],buffer.size()))>0){
    output.write(&buffer[0],bytesRead);
  } 
}   
于 2015-11-09T10:33:04.603 回答