
-----------------------------------
shoobyman
Wed Nov 26, 2008 4:40 pm

scanf
-----------------------------------
i have a problem with my program.  It needs to take input from the user to accept the user's address (as in their house, not a memory address).  The problem is that addresses have spaces in them and scanf does not read spaces.  So if they enter "123 crazy street" it would not work...
HELP PLEASE
I need it to take spaces as well.

-----------------------------------
md
Wed Nov 26, 2008 4:44 pm

RE:scanf
-----------------------------------
use fgets to read a line of input.

-----------------------------------
shoobyman
Wed Nov 26, 2008 4:51 pm

Re: scanf
-----------------------------------
but what is the syntax for it?

-----------------------------------
md
Wed Nov 26, 2008 6:09 pm

RE:scanf
-----------------------------------
Google "opengroup fgets" for documentation or man 3 fgets. 

Off hand I do not remember the syntax but I believe it is along the lines of fgets(buffer, max_size, FILE*);

-----------------------------------
Okapi
Wed Nov 26, 2008 6:33 pm

RE:scanf
-----------------------------------
http://www.cppreference.com/wiki/c/io/fgets

That site is a good reference site. (Hence the name ;))

Alternatively, you could make a char array and read in each character using getchar

while((i < 100) && (str[i++] = getchar()) != '\n'){
}

-----------------------------------
btiffin
Wed Nov 26, 2008 7:31 pm

RE:scanf
-----------------------------------
Umm, who told you scanf couldn't handle spaces? Get 'em!  ;)  

scanf and its friend fscanf, sscanf, vscanf, vfscanf, and vsscanf all handle whitespace quite automagically.

Read the man page again.

Not to say these are always the best functions for parsing strings, but scanf can do this job quite handily.

Cheers
Edit; typos and tone.

-----------------------------------
Okapi
Wed Nov 26, 2008 8:25 pm

RE:scanf
-----------------------------------
actually this:

#include 

int main(){

char string[100];
scanf("%s", &string);
printf("%s", &string);

return 0;
}


will only read to a space. If you input "this is a test" it will print "this".

-----------------------------------
btiffin
Wed Nov 26, 2008 9:44 pm

RE:scanf
-----------------------------------
Oops, sorry.  I was thinking and rats I feel like I'm giving away homework now, but I hope not:


int num = 0;
char street[32], type[32];
scanf(" %d %32s %32s \n", &num, street, type);
printf("%d %32s %32s\n", num, street, type);

Again,  rarely the best way to parse, but if you know your inputs are clean and conditions are right, this kinda code can save a fair amount of mucky muck.  Without clean inputs, this code is horribly broken.

And, as human input is involved, scanf is too fragile, so md's original advice is the right advice.

Cheers

-----------------------------------
deltatux
Sat Nov 29, 2008 3:41 pm

RE:scanf
-----------------------------------
if there's spaces in your input, you must use gets which is included inside 

deltatux

-----------------------------------
md
Sat Nov 29, 2008 3:59 pm

Re: RE:scanf
-----------------------------------
if there's spaces in your input, you must use gets which is included inside 

deltatux
DO NOT USE gets(). EVER.

gets() does not know how big your buffer is and will happily read well past it's end, leading to buffer overflows. gets() has been depreciated for that very reason.

fgets() is much the same as gets() except it takes a maximum number of characters to read in, thus preventing buffer overflows.
