views:

122

answers:

3

scanf("%s",str) won't do it. It will stop reading at the first space. gets(str) doesn't work either when the string is large. Any ideas?

+7  A: 

use fgets with STDIN as the file stream. Then you can specify the amount of data you want to read and where to put it.

SB
actually it's like this:
Johny
fgets(str1, 100, stdin); fgets(str2, 100, stdin); I won't get asked for the first string. It goes directly to the second one. Thanks for the quick answer.
Johny
I'm not sure what your comment means - is something not working?
SB
I won't get prompted to insert the first string.
Johny
Now I get prompted but the error window appears...:S
Johny
we don't have enough information to help you with these other issues you have. `fgets` is a blocking read, so you would do something like `printf("enter line here\n");` then call `fgets(...)` - fgets should not return until the user enters something with a newline
SB
That's what I did, but when I run the program, the error window appears before I enter anything... Anyway thanks ;)
Johny
what does the error window say/look like/etc?
SB
+1  A: 

When do you want to stop reading? At EOF, at a specific character, or what?

You can read a specific number of characters with %c

c Matches a sequence of width count characters (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating NUL is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

You can read specific characters (or up to excluded ones) with %[

[ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] charac- ter. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set ``everything except close bracket, zero through nine, and hyphen''. The string ends with the appearance of a character not in the (or, with a cir- cumflex, in) set or when the field width runs out

Paul
I just don't want blanks in between the string. I tried scanf("%[^\n]",str); but didn't work either. Thanks anyway!
Johny
So you want to read an entire line?
Paul
I guess but that means up to 79 chars, right? I need a string that may be larger.
Johny
A line to me means all characters upto a new line. Can you describe in more detail what you actually want to do here?
Paul
Ok. I want to do: char* str1;printf("Give a string\n"); scanf("%s",str1);char str2[strlen(str1)];for(int i=0; i<strlen(str1); i++) str2[i] = str1[i];Is this somehow helpful?
Johny
+1  A: 

Create your own function to read a line. Here's what you basically have to do:

1. fgets into allocated (growable) memory
2. if it was a full line you're done
3. grow the array
4. fgets more characters into the newly allocated memory
5. goto 2.

The implementation may be a bit tricky :-)

You need to think about what you need to pass to your function (at the very least the address of the array and its size); and what the function returns when everything "works" or when there is an error. You need to decide what is an error (is a string 10Gbytes long with no '\n' an error?). You need to decide on how to grow the array.


Edit

Actually it may be better to fgetc rather than fgets

get a character
it it EOF? DONE
add to array (update length), possible growing it (update size)
is it '\n'? DONE
repeat
pmg