简体   繁体   English

在 C++ 中以块的形式将蒸汽写入磁盘

[英]write a steam to disk in c++ in chunks

I am quite new to the C++ world, and I am trying to write a file stream to a disk.I am able to do the following:我对 C++ 世界很陌生,我正在尝试将文件流写入磁盘。我能够执行以下操作:

  const Aws::String objectKey2("filename.tar");
  Aws::S3::S3Client s3_client2;
  Aws::S3::Model::GetObjectRequest object_request2;
  object_request2.SetBucket(bucket_name);
  object_request2.SetKey(objectKey2);
  Aws::S3::Model::GetObjectOutcome get_object_outcome2 =
      s3_client2.GetObject(object_request2);
  auto &retrieved_file2 = get_object_outcome2.GetResult().GetBody();


  std::ios_base::sync_with_stdio(false);
  auto myfile = std::fstream("file.tar", std::ios::out | std::ios::binary);
  myfile.write((char*) &retrieved_file2, 1024*100);
  myfile.close();

But this is writing some 1k to disk and the file is unreadable (obviously).但是这是将一些 1k 写入磁盘并且文件不可读(显然)。 The file is a tar from from aws s3, which I try to write to disk in chunks.该文件是来自 aws s3 的 tar,我尝试将其分块写入磁盘。

Given the following...鉴于以下...

auto &retrieved_file2 = get_object_outcome2.GetResult().GetBody();

retrieved_file2 is actually of type Aws::IOStream & with Aws::IOStream being typedef'd via... retrieved_file2实际上是Aws::IOStream &类型, Aws::IOStream & Aws::IOStream通过...

typedef std::basic_iostream<char, std::char_traits<char>> Aws::IOStream;

Hence, you can't simply cast a pointer to retrieved_file2 to char * and hope that the result will make sense when written to a stream.因此,不能简单地将指针到retrieved_file2char *和希望,当写入数据流的结果才有意义。

If the result of the query is the contents of a tar file then you can probably just write the associated stream_buf using (untested)...如果查询的结果是 tar 文件的内容,那么您可能只需使用(未经测试)编写关联的stream_buf ...

std::fstream myfile("file.tar", std::ios::out | std::ios::binary);
myfile << retrieved_file2.rdbuf();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM