将unsigned char的向量写入二进制文件c ++

时间:2017-07-31 18:15:19

标签: c++ file io

我正在将二进制文件cmd.exe读入unsigned chars数组。读入bytes_read的总字节数是153.我将其转换为base64字符串,然后将此字符串解码回来(代码从第二个回答base64 decode snippet in c++)到向量<' BYTE>。这里BYTE是unsigned char。 decodeData.size()也是153.但是当我以二进制模式将此向量写入文件以再次获取我的cmd.exe文件时,我只获得1 KB文件。我错过了什么?

// Reading size of file
    FILE * file = fopen("cmd.exe", "r+");
    if (file == NULL) return 1;
    fseek(file, 0, SEEK_END);
    long int size = ftell(file);
    fclose(file);
// Reading data to array of unsigned chars
    file = fopen("cmd.exe", "r+");
    unsigned char * myData = (unsigned char *)malloc(size);
    int bytes_read = fread(myData, sizeof(unsigned char), size, file);
    fclose(file);

    std::string encodedData = base64_encode(&myData[0], bytes_read);
    std::vector<BYTE> decodedData = base64_decode(encodedData);

////write data to file
    ofstream outfile("cmd.exe", ios::out | ios::binary);
    outfile.write((const char *)decodedData.data(), decodedData.size());

更新: 感谢@chux建议&#34; r +&#34; - &GT; &#34; RB +&#34;问题解决了。

1 个答案:

答案 0 :(得分:2)

您将此标记为C ++。

这是一种使用fstream读取二进制文件的C ++方法。为了简化这个例子,我创建了一个比需要更大的m_buff。从评论中,听起来你的fopen(“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);
}

我系统上的一个典型输出如下:

/home/dmoen/.wine/drive_c/windows/system32/cmd.exe   722260 bytes  1180 us   
FINI   1417 us

您的结果会有所不同。

您的使用情况看起来不错(所以没有复制)。