Article ID: 119393
Article Last Modified on 4/24/2006
APPLIES TO
- Microsoft Visual C++ 1.0 Professional Edition
- Microsoft Visual C++ 1.5 Professional Edition
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52 Professional Edition
- Microsoft Visual C++ 2.0 Professional Edition
- Microsoft Visual C++ 4.0 Standard Edition
- Microsoft Visual C++ 4.1 Subscription
- Microsoft Visual C++ 4.2 Enterprise Edition
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 4.2 Professional Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++ 6.0 Standard Edition
- Microsoft Visual C++ .NET 2002 Standard Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
This article was previously published under Q119393
SYMPTOMS
If you extract an integer of an invalid octal value from istrstream, 0 is extracted and the ios::failbit is not set. In addition, the ios::fail() function returns FALSE, incorrectly indicating that the istrstream is valid.
RESOLUTION
Use the manipulators (dec, hex, oct, binary, text) explicitly to set the correct conversion base for the characters represented by the stream.
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
MORE INFORMATION
A scenario that produces an unexpected result, and which may also reflect this problem, involves using the string "08" to initialize the buffer used by the istrstream (or any string where the first character is 0 and one of the following characters is greater than 7).
This string causes the istrstream extraction operator to load the int with 0 instead of the expected value. This is because when 0 is the leading character in a number and it is not followed by an x or X, the number is treated as an octal number. Therefore, only the characters 0 through 7 after the leading 0 are valid.
Sample Code for Visual C++ 6.0 or earlier versions
/* Compile options needed: none */ #include <strstrea.h> void main() { char buffer[10] = "08"; int i; istrstream* str; str = new istrstream( buffer, 10 ); // The following extraction is made with the expectation that // the i will be set to 8. However, it will be set to 0. *str >> i; // To fix this, change the line to: // *str >> dec >> i; cout << "i = " << i << endl; cout << "str->fail()=" << str->fail() << endl; delete str; }
Sample Code for Visual C++ .NET 2002 or for Visual C++ .NET 2003
#include "stdafx.h" #include <strstream> using namespace std; void main() { char buffer[10] = "08"; int i; istrstream* str; str = new istrstream( buffer, 10 ); // The following extraction is made with the expectation that // the i will be set to 8. However, it will be set to 0. *str >> i; // To fix this, change the line to: // *str >> dec >> i; cout << "i = " << i << endl; cout << "str->fail()=" << str->fail() << endl; delete str; }
Additional query words: kbVC400bug 8.00 8.00c 9.00 9.10
Keywords: kbbug kblangcpp KB119393