0

It completely stops reading in code after it sees a space. How do I change my code so it reads in white space

char line[300];

printf("Enter a string to be checked: ");
scanf("%s", line);

the string I'm trying to input via redirection is:

( ( a a ) < > [ [ [ { [ x ] ]]] <>)

ShadyBears
  • 3,955
  • 13
  • 44
  • 66
  • 1
    possible duplicate of [How do I read white space using scanf in c?](http://stackoverflow.com/questions/3765023/how-do-i-read-white-space-using-scanf-in-c) – Karthik T Feb 21 '13 at 05:16
  • 1
    possible duplicate of [How do you allow spaces to be entered using scanf?](http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) – Some programmer dude Feb 21 '13 at 05:18

4 Answers4

2

You can use fgets intsead of scanf. For example:

    fgets(line, 1024, stdin);
someuser
  • 189
  • 1
  • 11
2

you should use fgets(line, size, stdin); as previously posted. you should never use gets(), as it expects a the same input size every time. Compilers, at least gcc, will warn you not to use it.

hbdavehb
  • 220
  • 1
  • 6
0

%s Matches a sequence of non-white-space characters; the next pointer must be a pointer to char, and the array must be large enough to accept all the sequence and the terminating NUL character. The input string stops at white space or at the maximum field width, whichever occurs first.

Need to choose a different format string. It's not really clear from your input string what exactly you're trying to accomplish.

Ian McMahon
  • 1,660
  • 11
  • 13
-1

Please check the code..

  printf("Enter a string to be checked: ");
    gets( line);
    puts(line);

Output:-

Enter a string to be checked: ( ( a a ) < > [ [ [ { [ x ] ]]] <>)
( ( a a ) < > [ [ [ { [ x ] ]]] <>)
Press any key to continue . . .
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Ravi Y Feb 21 '13 at 05:40
  • I have tested the code with author string. so please don't judge any answer without testing the bahaviour. – Akshay Joy Feb 21 '13 at 05:50