简体   繁体   English

如何将多个char读取组合到std :: vector中?

[英]How do I combine multiple char reads into a std::vector?

I'm reading multiple reports from a HID device into an unsigned char , then trying to copy the data to a std::vector . 我正在从HID设备读取多个报告到unsigned char ,然后尝试将数据复制到std::vector I'm also writing the data out to a file for hex analysis, whose content appears to be correct when I view it. 我也将数据写入文件进行十六进制分析,当我查看它时,其内容似乎是正确的。 However, the std::vector doesn't appear to contain the correct data when I dump it to the console. 但是,当我将其转储到控制台时, std::vector似乎不包含正确的数据。

This is the code: 这是代码:

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;
}

I have a feeling I'm way off the mark here; 我有一种感觉,我在这里不合时宜; I'm fairly new to C/C++, and I've been stuck with this for a while now. 我是C / C ++的新手,现在我已经坚持了一段时间。 Can anyone tell me what I'm doing wrong, or point me in a better direction? 任何人都可以告诉我我做错了什么,还是指出我的方向更好?

You want to add multiple unsigned char objects to your vector, but push_back only adds one. 您想要向向量添加多个unsigned char对象,但push_back只会添加一个。

So, replace retbuf.push_back(*outbuf); 所以,替换retbuf.push_back(*outbuf); with either: 与任何一个:

for (size_t i = 0; i < sizeof(outbuf); ++i) {
    retbuf.push_back(outbuf[i]);
}

or 要么

std::copy(outbuf, outbuf+sizeof(outbuf), std::back_inserter(retbuf));

or 要么

retbuf.insert(retbuf.end(), outbuf, outbuf+sizeof(outbuf));

which all do the same thing. 这一切都做同样的事情。

You create your vector with a certain size: 您可以使用特定大小创建矢量:

buffer_t retbuf(0x40 * reports);

but push_back increases the size of the vector by adding an element at the end. 但是push_back通过在末尾添加元素来增加向量的大小。 You should create it empty: 你应该创建它为空:

buffer_t retbuf;

Optionally, you could arrange for the vector to have enough space allocated, ready for the elements you're going to add: 或者,您可以安排向量分配足够的空间,为您要添加的元素做好准备:

retbuf.reserve(0x40 * reports);

This is purely a performance issue, but sometimes it's a significant issue for large vectors, or vectors of types that (unlike unsigned char ) are expensive to copy/move when the vector runs out of internal space and has to allocate more. 这纯粹是一个性能问题,但有时它对于大型向量来说是一个重要的问题,或者类型向量(与unsigned char不同)在向量耗尽内部空间并且必须分配更多时复制/移动是昂贵的。

A note on style: you repeat the literal value 0x40 a few times, and also use sizeof(outbuf) . 关于样式的注释:您重复几次字面值0x40 ,并使用sizeof(outbuf) It's often best to define a constant, and use the name throughout: 通常最好定义一个常量,并在整个过程中使用该名称:

const int report_size = 0x40;

This is partly in case the number changes in future, but also it's about the readability of your code -- if someone sees 0x40 they may or may not immediately understand why that is the correct value. 这部分是因为未来的数字会发生变化,而且还与代码的可读性有关 - 如果有人看到0x40他们可能会或可能不会立即理解为什么这是正确的值。 If someone sees report_size then they don't know what value that actually is until they look it up, but they do know why you're using that value. 如果有人看到report_size那么在他们查找之前他们不知道实际上是什么值,但他们确实知道你为什么要使用该值。

The problem is in this line: buffer_t retbuf(0x40 * reports); 问题出在这一行: buffer_t retbuf(0x40 * reports); It means that you create vector with 0x40 * reports elements filled with default value for unsigned char (zero). 这意味着您创建带有0x40 * reports向量0x40 * reports元素填充了unsigned char(零)的默认值。 Then push_back() just adds new elements to the end of vector and doesn't affect existing elements. 然后push_back()只是在向量的末尾添加新元素,而不会影响现有元素。

You need to rewrite it this way: 你需要以这种方式重写它:

buffer_t retbuf;                  // Empty vector
retbuf.reserve(0x40 * reports);   // Preallocate memory for known element count

This way push_back() will work as expected and add elements to empty vector from beginning. 这样push_back()将按预期工作,并从头开始向空向量添加元素。

And of course you shall push_back() all elements of outbuf , not only first one ( *outbuf ). 当然你应该push_back() outbuf所有元素,而不仅仅是第一个( *outbuf )。

To push back multiple values use std::vector's function assign. 要回退多个值,请使用std :: vector的函数assign。 For example: 例如:

std::vector<char>vec1;
char array[3] = {'a', 'b', 'c'};
vec1.assign(array, array+3);

I am currently working on a project were I had to do this. 我正在做一个项目我必须这样做。

Your vector is of a type unsigned char , which means every element of it is of this type. 您的向量是unsigned char类型,这意味着它的每个元素都属于这种类型。 Your outbuf is an array of unsigned chars. 你的outbuf是一无符号字符。

The push_back() only appends one item to the end of the vector, so push_back(*outbuf) will only add the first element of the outbuf to the vector, not all of them. push_back()只追加一个项目到向量的末尾,这样push_back(*outbuf)将在第一个元素只会增加outbuf的载体,不是所有的人。

To put all the data into the vector, you will need to push_back them one-by-one, or use std::copy . 要将所有数据放入向量中,您需要逐个push_back它们,或者使用std::copy

Note that since outbuf is a char array, then *outbuf will be the first element of the char array because of the array/pointer duality. 请注意,由于outbuf是一个char数组,因此* outbuf将是char数组的第一个元素,因为数组/指针的对偶性。

I think you probably wanted to do: 我想你可能想做:

typedef vector<string> buffer_t; // alternatively vector<unsigned char*>
...
retbuf.push_back(outbuf);
...

Or 要么

typedef vector<unsigned char> buffer_t;
...
for (size_t i = 0; i < sizeof(outbuf); i++)
     retbuf.push_back(outbuf);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM