• <legend id='0hfJ7'><style id='0hfJ7'><dir id='0hfJ7'><q id='0hfJ7'></q></dir></style></legend>

      • <bdo id='0hfJ7'></bdo><ul id='0hfJ7'></ul>
      <i id='0hfJ7'><tr id='0hfJ7'><dt id='0hfJ7'><q id='0hfJ7'><span id='0hfJ7'><b id='0hfJ7'><form id='0hfJ7'><ins id='0hfJ7'></ins><ul id='0hfJ7'></ul><sub id='0hfJ7'></sub></form><legend id='0hfJ7'></legend><bdo id='0hfJ7'><pre id='0hfJ7'><center id='0hfJ7'></center></pre></bdo></b><th id='0hfJ7'></th></span></q></dt></tr></i><div id='0hfJ7'><tfoot id='0hfJ7'></tfoot><dl id='0hfJ7'><fieldset id='0hfJ7'></fieldset></dl></div>
    1. <tfoot id='0hfJ7'></tfoot>

      <small id='0hfJ7'></small><noframes id='0hfJ7'>

        通过输入重定向读取二进制文件 c++ 的最佳方法

        Best way to read binary file c++ though input redirection(通过输入重定向读取二进制文件 c++ 的最佳方法)

            • <bdo id='S2k4f'></bdo><ul id='S2k4f'></ul>

                  <tbody id='S2k4f'></tbody>
              • <i id='S2k4f'><tr id='S2k4f'><dt id='S2k4f'><q id='S2k4f'><span id='S2k4f'><b id='S2k4f'><form id='S2k4f'><ins id='S2k4f'></ins><ul id='S2k4f'></ul><sub id='S2k4f'></sub></form><legend id='S2k4f'></legend><bdo id='S2k4f'><pre id='S2k4f'><center id='S2k4f'></center></pre></bdo></b><th id='S2k4f'></th></span></q></dt></tr></i><div id='S2k4f'><tfoot id='S2k4f'></tfoot><dl id='S2k4f'><fieldset id='S2k4f'></fieldset></dl></div>

              • <legend id='S2k4f'><style id='S2k4f'><dir id='S2k4f'><q id='S2k4f'></q></dir></style></legend>

                  <small id='S2k4f'></small><noframes id='S2k4f'>

                  <tfoot id='S2k4f'></tfoot>
                  本文介绍了通过输入重定向读取二进制文件 c++ 的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图在运行时读取一个大型二进制文件,认为输入重定向 (stdin),并且 stdin 是强制性的.

                  I am trying to read a large binary file thought input redirection (stdin) at runtime, and stdin is mandatory.

                  ./a.out < input.bin
                  

                  到目前为止我已经使用过 fgets.但是 fgets 会跳过空格和换行符.我想包括两者.我的 currentBuffersize 可以动态变化.

                  So far I have used fgets. But fgets skips blanks and newline. I want to include both. My currentBuffersize could dynamically vary.

                  FILE * inputFileStream = stdin; 
                  int currentPos = INIT_BUFFER_SIZE;
                  int currentBufferSize = 24; // opt
                  unsigned short int count = 0; // As Max number of packets 30,000/65,536
                  while (!feof(inputFileStream)) {
                      char buf[INIT_BUFFER_SIZE]; // size of byte
                      fgets(buf, sizeof(buf), inputFileStream);
                      cout<<buf;
                      cout<<endl;
                  }
                  

                  提前致谢.

                  推荐答案

                  如果是我,我可能会做类似的事情:

                  If it were me I would probably do something similar to this:

                  const std::size_t INIT_BUFFER_SIZE = 1024;
                  
                  int main()
                  {
                      try
                      {
                          // on some systems you may need to reopen stdin in binary mode
                          // this is supposed to be reasonably portable
                          std::freopen(nullptr, "rb", stdin);
                  
                          if(std::ferror(stdin))
                              throw std::runtime_error(std::strerror(errno));
                  
                          std::size_t len;
                          std::array<char, INIT_BUFFER_SIZE> buf;
                  
                          // somewhere to store the data
                          std::vector<char> input;
                  
                          // use std::fread and remember to only use as many bytes as are returned
                          // according to len
                          while((len = std::fread(buf.data(), sizeof(buf[0]), buf.size(), stdin)) > 0)
                          {
                              // whoopsie
                              if(std::ferror(stdin) && !std::feof(stdin))
                                  throw std::runtime_error(std::strerror(errno));
                  
                              // use {buf.data(), buf.data() + len} here
                              input.insert(input.end(), buf.data(), buf.data() + len); // append to vector
                          }
                  
                          // use input vector here
                      }
                      catch(std::exception const& e)
                      {
                          std::cerr << e.what() << '
                  ';
                          return EXIT_FAILURE;
                      }
                  
                      return EXIT_SUCCESS;
                  }
                  

                  请注意,您可能需要以二进制模式重新打开stdin,不确定它的可移植性如何,但各种文档表明跨系统的支持相当好.

                  Note you may need to re-open stdin in binary mode not sure how portable that is but various documentation suggests is reasonably well supported across systems.

                  这篇关于通过输入重定向读取二进制文件 c++ 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                      <tbody id='M2SKZ'></tbody>
                      <bdo id='M2SKZ'></bdo><ul id='M2SKZ'></ul>
                    • <i id='M2SKZ'><tr id='M2SKZ'><dt id='M2SKZ'><q id='M2SKZ'><span id='M2SKZ'><b id='M2SKZ'><form id='M2SKZ'><ins id='M2SKZ'></ins><ul id='M2SKZ'></ul><sub id='M2SKZ'></sub></form><legend id='M2SKZ'></legend><bdo id='M2SKZ'><pre id='M2SKZ'><center id='M2SKZ'></center></pre></bdo></b><th id='M2SKZ'></th></span></q></dt></tr></i><div id='M2SKZ'><tfoot id='M2SKZ'></tfoot><dl id='M2SKZ'><fieldset id='M2SKZ'></fieldset></dl></div>
                      <tfoot id='M2SKZ'></tfoot>
                    • <small id='M2SKZ'></small><noframes id='M2SKZ'>

                          <legend id='M2SKZ'><style id='M2SKZ'><dir id='M2SKZ'><q id='M2SKZ'></q></dir></style></legend>