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?
-
2`gets` never works. It's an inherent bug to ever use `gets`. Use `fgets`. – R.. GitHub STOP HELPING ICE Oct 26 '10 at 16:26
-
Maybe it's my version of c that won't accept gets at all...Thanks! – Johny Oct 26 '10 at 16:28
-
@Johnny: R's admonition against using `gets()` is due to the fact that using it introduces a *huge* security hole in your code, not that your particular compiler has an issue with it (it shouldn't; `gets()` has been part of the C since the beginning). It's been deprecated as of C99 and should no longer be used. – John Bode Oct 26 '10 at 19:47
5 Answers
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.

- 22,560
- 5
- 55
- 61
-
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 Oct 26 '10 at 16:27
-
-
-
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 – NG. Oct 26 '10 at 17:09
-
That's what I did, but when I run the program, the error window appears before I enter anything... Anyway thanks ;) – Johny Oct 26 '10 at 17:18
char str[100];
Try this
scanf("%[^\n]s",str);
or this
fgets(str, sizeof str, stdin))

- 488
- 2
- 7
- 15
-
11) There is not reason for `s` in `"%[^\n]s"`. 2) `scanf("%[^\n]...` will scan nothing and save nothing in `str` if input is `"\n"`. 3) `scanf("%[^\n]...` does not protect against large input. 4) `scanf("%[^\n]...` leaves `'\n'` in `stdin`. – chux - Reinstate Monica Jan 10 '15 at 18:53
-
1Also the `s` does not make sense here. `%[^\n]` already asks for all characters but a new-line. – alk Nov 25 '18 at 18:58
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

- 106,608
- 13
- 126
- 198
To read string with space you can do as follows:
char name[30],ch;
i=1;
while((ch=getchar())!='\n')
{
name[i]=ch;
i++;
}
i++;
name[i]='\n';
printf("String is %s",name);

- 20,559
- 3
- 52
- 104

- 11
- 1
-
1`ch` should be `int` to distinguish `EOF` from all other `char`. As it is now, `while((ch=getchar())!='\n')` is an infinite loop should `EOF` be return from `getchar()`. – chux - Reinstate Monica Jan 10 '15 at 18:58
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

- 41,321
- 20
- 104
- 134
-
I just don't want blanks in between the string. I tried scanf("%[^\n]",str); but didn't work either. Thanks anyway! – Johny Oct 26 '10 at 16:31
-
I guess but that means up to 79 chars, right? I need a string that may be larger. – Johny Oct 26 '10 at 16:38
-
1A line to me means all characters upto a new line. Can you describe in more detail what you actually want to do here? – The Archetypal Paul Oct 26 '10 at 16:48
-
Ok. I want to do: char* str1; printf("Give a string\n"); scanf("%s",str1); char str2[strlen(str1)]; for(int i=0; i
– Johny Oct 26 '10 at 16:55 -