Reputation: 1137
I am writing a video encoder, I want to copy buffers using vectors for some reason std::copy
is only copying the first character. my goal is to copy the buffer and write them to a file using fwrite
.
I am using Google Native Client which does not allow me to have fwrite
directly from the main thread, so I created a new thread and I have to move the buffer to fwrite
I tagged multi threading with this questions to get your feedback on this approach dealing with buffers.
std::vector<char> buffer_(4);
char header[4];
header[0] = 'D';
header[1] = 'K';
header[2] = 'I';
header[3] = 'F';
unsigned dataArraySize = sizeof(header) / sizeof(char);
std::copy(&header[0], &header[dataArraySize], buffer_.begin());
std::stringstream ss;
std::string s;
for (std::vector<char>::iterator it = buffer_.begin(); it!=buffer_.end(); ++it)
{
char c = *it;
ss << c;
ss >> s;
Logger::Log(s);
}
For the output I only get D four times
Upvotes: 0
Views: 2178
Reputation: 217085
The copy is fine, it is the conversion from char to string which is wrong, you may just use the appropriate constructor
for (std::vector<char>::iterator it = buffer_.begin(); it!=buffer_.end(); ++it)
{
char c = *it;
Logger::Log(std::string(1, c));
}
or simply
for (char c : buffer_) {
Logger::Log(std::string(1, c));
}
And, potentially, depending of your logger, concatenate to form one unique string:
Logger::Log(std::string(buffer_.data(), buffer_.size()));
Upvotes: 2