Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to stdout using character array (not null terminated) c/c++

Tags:

c++

stdout

What's the most straightforward way to write to stdout using a character array? I want to output a slice of a much larger array, and the slice is not null-terminated. I want to avoid copying the slice to a "proper" null-terminated c-string.

like image 541
chowey Avatar asked Aug 28 '13 18:08

chowey


1 Answers

There is a pretty obvious solution I didn't find at first. std::cout is an instance of ostream.

void WriteChunk(char *buffer, size_t startpos, size_t length) {
    std::cout.write(buffer + startpos, length);
}

so std::cout.write does the trick.

like image 66
chowey Avatar answered Nov 15 '22 21:11

chowey