istream& istream::operator>>(int& n)
{
_WINSTATIC char ibuffer[MAXLONGSIZ];
long value;
char ** endptr = (char**)NULL;
if (ipfx(0))
{
value = strtol(ibuffer, endptr, getint(ibuffer));
if (value>INT_MAX)
{
n = INT_MAX;
state |= ios::failbit;
}
else if (value<INT_MIN)
{
n = INT_MIN;
state |= ios::failbit;
}
else
n = (int) value;
isfx();
}
return *this;
}
当调用cin<<ch时,程序会调用以上程序,注意value = strtol(ibuffer, endptr, getint(ibuffer))这行语句中getint(ibuffer)会调用如下程序
int istream::getint(char * buffer) // returns length
{
int base, i;
int c;
int fDigit = 0;
int bindex = 1;
if (x_flags & ios::dec)
base = 10;
else if (x_flags & ios::hex)
base = 16;
else if (x_flags & ios::oct)
base = 8;
else
base = 0;
if (ipfx(0))
{
c=bp->sgetc();
for (i = 0; i<MAXLONGSIZ-1; buffer[i] = (char)c,c=bp->snextc(),i++)
{
if (c==EOF)
{
state |= ios::eofbit;
break;
}
if (!i)
{
if ((c=='-') || (c=='+'))
{
bindex++;
continue;
}
}
if ((i==bindex) && (buffer[i-1]=='0'))
{
if (((c=='x') || (c=='X')) && ((base==0) || (base==16)))
{
base = 16; // simplifies matters
fDigit = 0;
continue;
}
else if (base==0)
{
base = 8;
}
}
// now simply look for a digit and set fDigit if found else break
if (base==16)
{
if (!isxdigit(c))
break;
}
else if ((!isdigit(c)) || ((base==8) && (c>'7')))
break;
fDigit++;
}
if (!fDigit)
{
state |= ios::failbit;
while (i--)
{
if(bp->sputbackc(buffer[i])==EOF)
{
state |= ios::badbit;
break;
}
else
state &= ~(ios::eofbit);
}
i=0;
}
// buffer contains a valid number or '\0'
buffer[i] = '\0';
isfx();
}
if (i==MAXLONGSIZ)
{
state |= ios::failbit;
}
return base;
}