我正在將多個報告從HID設備讀取到unsigned char
,然後嘗試將數據複製到std::vector
。我還將數據寫入文件進行十六進制分析,當我查看它時,其內容似乎是正確的。但是,當我將其轉儲到控制檯時,std::vector
似乎不包含正確的數據。如何將多個char讀取結合到一個std :: vector中?
這是代碼:
typedef vector<unsigned char> buffer_t;
buffer_t sendCommand (hid_device *devh, const unsigned char cmd[], int reports) {
unsigned char outbuf[0x40];
buffer_t retbuf(0x40 * reports);
hid_write(devh, cmd, 0x41);
int i;
FILE *file = fopen("test.out", "w+b");
while (i++ < reports) {
hid_read(devh, outbuf, 0x40);
fwrite(outbuf, 1, sizeof(outbuf), file);
retbuf.push_back(*outbuf);
}
fclose(file);
cout << &retbuf[0];
return retbuf;
}
我有一種感覺,我的方式在這裏下車的標誌;我對C/C++相當陌生,而且我一直堅持這一段時間。任何人都可以告訴我我做錯了什麼,或者指出我的方向更好嗎?
您的載體將含有outbuf中只有第一個字符,因爲你正在使用* outbuf中。 – Shubhansh