Vojto Vojto - 1 year ago 80
Objective-C Question

Trying to make NSString from scanf. Only getting the first word

Here's what I'm trying to do:

char inputBuffer[200];
NSString *input;
scanf("%s", inputBuffer);
input = [[NSString alloc] initWithUTF8String:inputBuffer];

Here's what happens:

bla bla bla
2010-11-08 23:17:03.670 TextGame[51147:a0f] bla

inputBuffer contains whole string. I tried to do
NSLog("%@", inputBuffer)
and there was whole string.

When I create this NSString it looks like it contains only the last word.

Please help me out with these I Googled like the whole internet and didn't get anywhere. :-)


scanf("%s", inputBuffer); only reads a characters until a space is encountered. Therefore you get only "bla" stored in the buffer. You should use gets(inputBuffer). This function "Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached."