Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将无符号字符的向量写入二进制文件c++;_C++_File_Io - Fatal编程技术网

C++ 将无符号字符的向量写入二进制文件c++;

C++ 将无符号字符的向量写入二进制文件c++;,c++,file,io,C++,File,Io,我正在将二进制文件cmd.exe读入无符号字符数组。读取到字节的总字节数为153。我将它转换为Base64字符串,然后将该字符串(从第二答案)解码为vector ,将此标记为C++。p> 这是一个使用FScript读取二进制文件的C++方法。为了简化这个例子,我创建了一个比需要的更大的m_buff。从注释中,听起来好像你的fOnter(“CMD.exe”,“R+”)是错误的,所以我只提供C++二进制读。 方法tReader()a)以二进制模式打开文件,b)将数据读入m_buff,c)捕获gCou

我正在将二进制文件cmd.exe读入无符号字符数组。读取到字节的总字节数为153。我将它转换为Base64字符串,然后将该字符串(从第二答案)解码为vector ,将此标记为C++。p>

这是一个使用FScript读取二进制文件的C++方法。为了简化这个例子,我创建了一个比需要的更大的m_buff。从注释中,听起来好像你的fOnter(“CMD.exe”,“R+”)是错误的,所以我只提供C++二进制读。 方法tReader()a)以二进制模式打开文件,b)将数据读入m_buff,c)捕获gCount以显示

它还演示了一种可能使用计时来测量持续时间的方法

#include <chrono>
// 'compressed' chrono access --------------vvvvvvv
typedef std::chrono::high_resolution_clock  HRClk_t; 
typedef HRClk_t::time_point                 Time_t;  
typedef std::chrono::microseconds           US_t;    
using   namespace std::chrono_literals;  // suffixes 100ms, 2s, 30us

#include <iostream>
#include <fstream>
#include <cassert>

class T516_t
{
   enum BuffConstraints : uint32_t {
      Meg           = (1024 * 1024),
      END_BuffConstraints
   };

   char*   m_buff;
   int64_t m_gCount;

public:

   T516_t()
      : m_buff (nullptr)
      , m_gCount (0)
      {
         m_buff = new char[Meg];
      }

   ~T516_t() = default;

   int exec()
      {
         tReader();
         return(0);
      }

private: // methods

   void tReader()
      {
         std::string pfn = "/home/dmoen/.wine/drive_c/windows/system32/cmd.exe";
         // open file in binary mode
         std::ifstream sIn (pfn, std::ios_base::binary);

         if (!sIn.is_open()) {
            std::cerr << "UNREACHABLE:  unable to open sIn " << pfn
                      << " priviledges? media offline?";
            return;
         }

         Time_t start_us = HRClk_t::now();
         do
         {
            // perform read
            sIn.read (m_buff, Meg);
            // If the input sequence runs out of characters to extract (i.e., the
            // end-of-file is reached) before n characters have been successfully
            // read, buff contains all the characters read until that point, and
            // both eofbit and failbit flags are set

            m_gCount = sIn.gcount();

            if(sIn.eof()) { break; } // exit when no more data

            if(sIn.failbit ) {
               std::cerr << "sIn.faileBit() set" << std::endl;
            }

         }while(1);
         auto  duration_us = std::chrono::duration_cast<US_t>(HRClk_t::now() - start_us);

         sIn.close();

         std::cout << "\n  " << pfn
                   << "   " << m_gCount << " bytes"
                   << "   " << duration_us.count() << " us"
                   << std::endl;

      } // int64_t tReader()

}; // class T516_t

int main(int , char**)
{
   Time_t start_us = HRClk_t::now();

   int retVal = -1;
   {
      T516_t   t516;
      retVal = t516.exec();
   }
   auto  duration_us = std::chrono::duration_cast<US_t>(HRClk_t::now() - start_us);

   std::cout << "  FINI   " << duration_us.count() << " us" << std::endl;
   return(retVal);
}
你的结果会有所不同


您的流使用看起来不错(因此没有复制)。

1KB可能是最小的非空文件。。。你看了文件吗?为什么在C++代码中使用FOpenE?如果是代码> CMD.exe < /Cube >是二进制的,它也应该是二进制模式读取的。甚至比使用<代码> fOpen< <代码>和<代码> FRADE<代码>在C++中,为什么你使用C++中的代码> MalOC ?建议“代码>”R+“/>代码> ->代码>“Rb+”< /C>
#include <chrono>
// 'compressed' chrono access --------------vvvvvvv
typedef std::chrono::high_resolution_clock  HRClk_t; 
typedef HRClk_t::time_point                 Time_t;  
typedef std::chrono::microseconds           US_t;    
using   namespace std::chrono_literals;  // suffixes 100ms, 2s, 30us

#include <iostream>
#include <fstream>
#include <cassert>

class T516_t
{
   enum BuffConstraints : uint32_t {
      Meg           = (1024 * 1024),
      END_BuffConstraints
   };

   char*   m_buff;
   int64_t m_gCount;

public:

   T516_t()
      : m_buff (nullptr)
      , m_gCount (0)
      {
         m_buff = new char[Meg];
      }

   ~T516_t() = default;

   int exec()
      {
         tReader();
         return(0);
      }

private: // methods

   void tReader()
      {
         std::string pfn = "/home/dmoen/.wine/drive_c/windows/system32/cmd.exe";
         // open file in binary mode
         std::ifstream sIn (pfn, std::ios_base::binary);

         if (!sIn.is_open()) {
            std::cerr << "UNREACHABLE:  unable to open sIn " << pfn
                      << " priviledges? media offline?";
            return;
         }

         Time_t start_us = HRClk_t::now();
         do
         {
            // perform read
            sIn.read (m_buff, Meg);
            // If the input sequence runs out of characters to extract (i.e., the
            // end-of-file is reached) before n characters have been successfully
            // read, buff contains all the characters read until that point, and
            // both eofbit and failbit flags are set

            m_gCount = sIn.gcount();

            if(sIn.eof()) { break; } // exit when no more data

            if(sIn.failbit ) {
               std::cerr << "sIn.faileBit() set" << std::endl;
            }

         }while(1);
         auto  duration_us = std::chrono::duration_cast<US_t>(HRClk_t::now() - start_us);

         sIn.close();

         std::cout << "\n  " << pfn
                   << "   " << m_gCount << " bytes"
                   << "   " << duration_us.count() << " us"
                   << std::endl;

      } // int64_t tReader()

}; // class T516_t

int main(int , char**)
{
   Time_t start_us = HRClk_t::now();

   int retVal = -1;
   {
      T516_t   t516;
      retVal = t516.exec();
   }
   auto  duration_us = std::chrono::duration_cast<US_t>(HRClk_t::now() - start_us);

   std::cout << "  FINI   " << duration_us.count() << " us" << std::endl;
   return(retVal);
}
/home/dmoen/.wine/drive_c/windows/system32/cmd.exe   722260 bytes  1180 us   
FINI   1417 us