Subj : Re: sscanf and bool To : borland.public.cpp.borlandcpp From : Greg Chicares Date : Tue Aug 24 2004 12:06 pm Jogy wrote: > MarvinAvery wrote: > >> I have a character string that contains some boolean values: eg. "0010" >> I want to read these values via the sscanf() function: eg. >> bool b1, b2, b3, b4; >> sscanf (line, "%1d%1d%1d%1d", &b1, &b2, &b3, &b4); >> >> Problem is "%1d" stores the value as int -- that is as 16 bits whereas >> the >> storage location is only 8 bits. So I am getting a buffer overflow. >> >> Is there any conversion format that will read and store an 8-bit integer? >> Any other suggestions? Avoid sscanf(): as your example shows, it's unsafe. Do you need anything fancier than bool b0 = "0" != values[0] ... bool b3 = "0" != values[3] after making sure you have exactly four characters to convert? > I think %c is the printf/scanf format for 8-bit variable. Isn't "%c" for one-byte characters? > But I am not sure if the current C++ standard specifies that > the size of bool is 8-bit ..... sizeof(bool) is implementation-defined: 5.3.3/1 . .