Microsoft KB Archive/149966

= FIX: istream::operator>>(long&) Does Not Clear errno =

Article ID: 149966

Article Last Modified on 12/10/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q149966

<div class="symptoms_section">

SYMPTOMS
If an error occurs when extracting long values from the stream, the error cannot be cleared and other streams also report errors.

<div class="cause_section">

CAUSE
When the istream::operator>>(long& n) encounters an error, it sets ios::failbit, but does not clear errno. This causes the stream to continue to fail even after ios::clear is called. It also causes other streams to fail.

<div class="resolution_section">

RESOLUTION
To work around the bug, set errno to zero in addition to completing normal stream error checking.

<div class="status_section">

STATUS
This bug was corrected in Microsoft Visual C++ .NET 2003.

<div class="moreinformation_section">

Sample Code
/* The following sample code demonstrates only the bug and workaround, not normal error checking.

Compile options needed: none

//#define    CLEAR_ERRNO    // uncomment to work around bug
 * 1) include   <iostream.h>
 * 2) include   <strstrea.h>
 * 3) include   <stdio.h>
 * 4) include   <stdlib.h>
 * 5) include   <string.h>
 * 6) define    MAX_STRING_LEN      100

void main {   char    dc1a, dc1b, dc2a, dc2b; long   input1a=0, input1b=0; long   input2a=0, input2b=0; char   szTest1[MAX_STRING_LEN]; char   szTest2[MAX_STRING_LEN];

strcpy(szTest1,"a2147483648z50"); // strcpy(szTest2,"a500z50");

istrstream   StrStream1(szTest1); //String stream 1 istrstream   StrStream2(szTest2); //String stream 2

//   The following command will fail since value input to    //    input1a=2147483648 is    //    bigger that maximum value long can hold.

StrStream1 >> dc1a >> input1a >> dc1b >> input1b; errno = 0; StrStream2 >> dc2a >> input2a >> dc2b >> input2b;
 * 1) ifdef   CLEAR_ERRNO
 * 1) endif

cout << "\n   dc1a: " << dc1a << "\n input1a: " << input1a << "\n   dc1b: " << dc1b << "\n input1b: " << input1b << endl;

cout << "\n   dc2a: " << dc2a << "\n input2a: " << input2a << "\n   dc2b: " << dc2b << "\n input2b: " << input2b << endl;

cout << "\n\nPress any key to continue..." << endl; dc1a = getchar; }

/* The output is: dc1a: a input1a: 2147483647 dc1b: input1b: 0

dc2a: a input2a: 500 dc2b: input2b: 0

If CLEAR_ERRNO is set, the output is as expected: dc1a: a input1a: 2147483647 dc1b: input1b: 0

dc2a: a input2a: 500 dc2b: z input2b: 50

Additional query words: kbVC400bug

Keywords: kbfix kbcrt KB149966

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.