2017-07-31 87 views
0

我正在讀取二進制文件cmd.exe到無符號字符數組。讀入bytes_read的總字節數爲153.我將它轉換爲base64字符串,然後將此字符串解碼(從第二個答案base64 decode snippet in c++開始)到矢量<'BYTE>中。這裏BYTE是無符號字符。 decodedData.size()也是153.但是,當我將這個向量寫入二進制模式文件以再次獲取我的cmd.exe文件時,我只能得到1 KB文件。我錯過了什麼?寫入無符號字符向量到二進制文件C++

// 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的建議 「R +」 - > 「RB +」 問題解決。

+0

我在c#中做了所有這些工作。上面的代碼中缺少什麼東西? –

+0

函數fread的返回值爲153 –

+1

1 kb可能是最小可能的非空文件...你看過這個文件嗎? –

回答

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 

你的結果會有所不同。

您的流媒體使用看起來不錯(所以沒有複製)。