home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_10_10
/
1010026a
< prev
next >
Wrap
Text File
|
1992-08-10
|
566b
|
31 lines
#include <stdio.h>
#include <string.h>
#define BUFSIZE 64
#define MAXBITS 15 /* Largest value allowed is 32767 */
int fgetb(FILE *fp)
{
int i;
unsigned sum = 0, value = 1;
char buf[BUFSIZE];
if (!fp || fscanf(fp," %[01]",buf) != 1 ||
strlen(buf) > MAXBITS)
return EOF;
for (i = strlen(buf) - 1; i >= 0; --i)
{
if (buf[i] == '1')
sum += value;
value *= 2;
}
return sum;
}
main()
{
int n = fgetb(stdin);
printf("The number was %d\n",n);
return 0;
}