As stated in What is the C ++ idiom for replacing snprintf (3)? , I parse the file header and create an error message, the byte field in the header is damaged. This code summarizes what I'm trying to do:
const std::string parseCapturePattern(const int fd) { // fd is the descriptor of the ogg file const char CAPTURE_PATTERN[4] = {'O', 'g', 'g', 'S'}; char capture_pattern[4]; read(fd, capture_pattern, sizeof(capture_pattern)); // error handling omitted if (strncmp(capture_pattern, CAPTURE_PATTERN, sizeof(capture_pattern)) != 0) { std::ostringstream err; /*** This won't actually work ***/ err_msg << "Invalid capture pattern: '" << capture_pattern << "'; expecting '" << CAPTURE_PATTERN << "'"; /*** This won't actually work ***/ return err.str(); } }
This will not work because capture_pattern and capture_pattern are not character arrays with a NULL character. What kind of work is this:
err_msg << "Invalid capture pattern: '" << capture_pattern[0] << capture_pattern[1] << capture_pattern[2] << capture_pattern[3] << "'; expecting '" << CAPTURE_PATTERN[0] << CAPTURE_PATTERN[1] << CAPTURE_PATTERN[2] << CAPTURE_PATTERN[3] << "'";
This, of course, is almost indescribably disgusting.
Any ideas?
source share