...
To solve this problem, it may be tempting to use the std::ios_base::width() method, but there still is a trap, as shown in this noncompliant code example:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <iostream>
void f() {
char bufOne[12];
char bufTwo[12];
std::cin.width(12);
std::cin >> bufOne;
std::cin >> bufTwo;
} |
In . In this example, the first read will not overflow, but the second read still could. The C++ Standard, [istream.extractors], paragraphs 7–9, describes the behavior of of operator>>(basic_istream &, charT *), and states in part part [ISO/IEC 14882-2014]:
operator>>then stores a null byte (charT()) in the next position, which may be the first position if no characters were extracted.operator>>then calls then callswidth(0).
Consequently, it is necessary to call call width() prior to each each operator>> call call passing a bounded array.
...
| bgColor | #ffcccc |
|---|---|
| lang | cpp |
...
.
...
Noncompliant Code Example
...