How can I write vector<unsigned char> to file, followed by an unsigned int
<p>I have to group 5 <code>unsigned char</code> vectors (<code>data_length=5</code>). At the end of the group, I want to add an ID group and write them (group data+ its ID) to file. In which group ID types <code>integer</code>. I do that task as below code. Is it correct? 
 The bellow figure  shows what I am taking</p>

<p><a href="https://i.stack.imgur.com/5uPGk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5uPGk.png" alt="enter image description here"></a></p>

<pre><code>#define random(x) (rand()%x)
std::ofstream filewrite("abc.raw", std::ios::out | std::ofstream::binary);
unsigned int iter = 0;
unsigned int data_length=5;
unsigned int ID_data=0;
//-------------Write data-------------//
while (iter&lt;10){
    vector&lt;unsigned char&gt; vec_data;
    for (unsigned int i=0;i&lt;data_length;i++){
        vec_data.push_back(random(256))
    }           
    std::copy(vec_data.begin(), vec_data.end(), std::ostreambuf_iterator&lt;char&gt;(filewrite));
    //Write ID_data after vec_data
    filewrite.write(reinterpret_cast&lt;const char *&gt;(&amp;ID_data), sizeof(ID_data));
    ID_data++;
    iter++;
}
filewrite.close();
</code></pre>

<p>In additions, I want to extract the data to vector without the group ID. This is my code to extract data from the above file, but it does not remove the ID group. Could you help me to remove it?  </p>

<pre><code>//-------------Read data-------------//
std::ifstream file("abc.raw", std::ios::binary);
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);

// get its size:
std::streampos fileSize;

file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);

// reserve capacity
std::vector&lt;unsigned char&gt; vec;
     vec.insert(vec.begin(),
std::istream_iterator&lt;unsigned char&gt;(file),
std::istream_iterator&lt;unsigned char&gt;());
</code></pre>