Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 scanf
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Wed Nov 26, 2008 4:44 pm   Post subject: RE:scanf

use fgets to read a line of input.
shoobyman




PostPosted: Wed Nov 26, 2008 4:51 pm   Post subject: Re: scanf

but what is the syntax for it?
md




PostPosted: 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*);
Okapi




PostPosted: 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 Wink)

Alternatively, you could make a char array and read in each character using getchar

while((i < 100) && (str[i++] = getchar()) != '\n'){
}
btiffin




PostPosted: Wed Nov 26, 2008 7:31 pm   Post subject: RE:scanf

Umm, who told you scanf couldn't handle spaces? Get 'em! Wink

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




PostPosted: 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".
btiffin




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
deltatux




PostPosted: 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
md




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: