Amazon.com Widgets How to : scanf() - strings
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 23, 2014, 02:13:11 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 5 - Program Looping
| | | |-+ How to : scanf() - strings
Pages: [1] Go Down
Print
Author Topic: How to : scanf() - strings (Read 4350 times)
elzix88
Newbie
*
Posts: 8


Email




on: September 22, 2010, 10:11:05 AM

Can anyone tell me how to use scanf (or another?) function for input string from console? I tried like this but it works only if string doesn't have white space between words. Is it any possibility to do this?


Here is part of code:
Code: (Objective-C)
NSMutableString *m1;
char name[256];
scanf("%s",&name);           // i enter here string "Baba jaga" for example
m1 = [NSString stringWithCString: name encoding: NSASCIIStringEncoding];
NSLog(@"%@",m1);          // output is only "Baba"
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: September 22, 2010, 05:30:11 PM

Try gets (name);

Cheers,

Steve
Logged
elzix88
Newbie
*
Posts: 8


Email




Reply #2 on: September 23, 2010, 10:57:07 AM

Thank you for your reply. That solution works good but compiler displays message while running program:

Quote
warning: this program uses gets(), which is unsafe.

why its unsafe? how can i delete this warning?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: September 23, 2010, 05:15:05 PM

Probably because it doesn't check the count of the number of characters entered,  meaning if you type enough you'll overflow the input buffer.   There have been many attacks made to software programs by these sorts of means.

Cheers,

Steve Kochan
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.