scanf
Author |
Message |
shoobyman
![](http://compsci.ca/v3/uploads/user_avatars/1894631788463006b596f19.gif)
|
Posted: Wed Nov 26, 2008 4:40 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Wed Nov 26, 2008 4:44 pm Post subject: RE:scanf |
|
|
use fgets to read a line of input. |
|
|
|
|
![](images/spacer.gif) |
shoobyman
![](http://compsci.ca/v3/uploads/user_avatars/1894631788463006b596f19.gif)
|
Posted: Wed Nov 26, 2008 4:51 pm Post subject: Re: scanf |
|
|
but what is the syntax for it? |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Wed Nov 26, 2008 6:09 pm Post subject: 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*); |
|
|
|
|
![](images/spacer.gif) |
Okapi
|
Posted: Wed Nov 26, 2008 6:33 pm Post subject: 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'){
} |
|
|
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
Posted: Wed Nov 26, 2008 7:31 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Okapi
|
Posted: Wed Nov 26, 2008 8:25 pm Post subject: RE:scanf |
|
|
actually this:
code: | #include <stdio.h>
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". |
|
|
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
Posted: Wed Nov 26, 2008 9:44 pm Post subject: RE:scanf |
|
|
Oops, sorry. I was thinking and rats I feel like I'm giving away homework now, but I hope not:
code: |
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 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
deltatux
![](http://compsci.ca/v3/uploads/user_avatars/2510662304931ac0b5cb22.png)
|
Posted: Sat Nov 29, 2008 3:41 pm Post subject: RE:scanf |
|
|
if there's spaces in your input, you must use gets which is included inside <string.h>
deltatux |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Sat Nov 29, 2008 3:59 pm Post subject: Re: RE:scanf |
|
|
deltatux @ 2008-11-29, 3:41 pm wrote: if there's spaces in your input, you must use gets which is included inside <string.h>
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. |
|
|
|
|
![](images/spacer.gif) |
|
|