Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from input until newline is found using scanf()?

Tags:

I was asked to do a work in C when I'm supposed to read from input until there's a space and then until the user presses enter. If I do this:

scanf("%2000s %2000s", a, b); 

It will follow the 1st rule but not the 2nd.
If I write:

I am smart

What I get is equivalent to:
a = "I";
b = "am";
But It should be:
a = "I";
b = "am smart";

I already tried:

scanf("%2000s %2000[^\n]\n", a, b); 

and

scanf("%2000s %2000[^\0]\0", a, b); 

In the 1st one, it waits for the user to press Ctrl+D (to send EOF) and that's not what I want. In the 2nd one, it won't compile. According to the compiler:

warning: no closing ‘]’ for ‘%[’ format

Any good way to solve this?

like image 956
brunoais Avatar asked Nov 11 '11 17:11

brunoais


People also ask

Does scanf read until newline?

scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.

How do I ignore a new line in scanf?

For a simple solution, you could add a space before the format specifier when you use scanf(), for example: scanf(" %c", &ch); The leading space tells scanf() to skip any whitespace characters (including newline) before reading the next character, resulting in the same behavior as with the other format specifiers.

How do I read a new line in scanf?

We can make scanf() to read a new line by using an extra \n, i.e., scanf(“%d\n”, &x) . In fact scanf(“%d “, &x) also works (Note the extra space). We can add a getchar() after scanf() to read an extra newline.


1 Answers

scanf (and cousins) have one slightly strange characteristic: white space in (most placed in) the format string matches an arbitrary amount of white space in the input. As it happens, at least in the default "C" locale, a new-line is classified as white space.

This means the trailing '\n' is trying to match not only a new-line, but any succeeding white-space as well. It won't be considered matched until you signal the end of the input, or else enter some non-white space character.

One way to deal with that is something like this:

scanf("%2000s %2000[^\n]%c", a, b, c);  if (c=='\n')     // we read the whole line else     // the rest of the line was more than 2000 characters long. `c` contains a      // character from the input, and there's potentially more after that as well. 

Depending on the situation, you might also want to check the return value from scanf, which tells you the number of conversions that were successful. In this case, you'd be looking for 3 to indicate that all the conversions were successful.

like image 81
Jerry Coffin Avatar answered Sep 21 '22 16:09

Jerry Coffin