Little embarrassment of gets () and scanf (), getsscanf
Gets () and scanf () are a little awkward to get along with, and gets () is a mess after scanf (). Why, first understand the similarities and differences between them:
Different: Both can accept continuous character data and automatically add '\ 0' after the end of the character to mark the end of acceptance
different:
Scanf cannot accept spaces, tabs, carriage returns, etc., and ends when it encounters a space
Gets can accept spaces, tabs, and carriage returns, etc., and will end when a carriage return or EOF (end of file) is encountered
GetsWhen gets () is after scanf (), after entering scanf () and pressing enter, gets () receives the enter key. The key is that they use different end tags. When inputting a string, scanf () encounters spaces, carriage returns, and tab ends, but these terminator characters are still left in the buffer. If you then use gets () to get the next line of strings, it encounters The carriage return left before (or there are blank characters such as spaces before the carriage return), then this time gets () will be invalidated. Therefore, the phenomenon that the first string often encountered becomes a blank string appears. I always encountered it before, so I took a turn to solve this problem.
I use the function sscanf () (a function that reads data from a string in a format that matches the specified format in C) to solve the problem of the carriage return being eaten. About sscanf (), the function format header file This is described in stdio.h:
_Check_return_ _CRT_INSECURE_DEPRECATE (sscanf_s)
_CRT_STDIO_INLINE int __CRTDECL sscanf (
_In_z_ char const * const _Buffer,
_In_z_ _Scanf_format_string_ char const * const _Format,
...)
Sscanf (read into a string, specify the format that matches the string, variable data list)
Give me a millet
#include <stdio.h>
int main (void)
{
int d;
char c, a [100] = "365hello", b [100];
sscanf (a, "% d% c% s", & d, & c, b);
printf ("% d% c% s", d, c, b);
}
Run to print out the result 365 h ello, these are in accordance with the specified format. It is also through this that I use two gets at the same time. The first one is used to read the character information obtained in scanf (), and then use sscanf () to extract the available data. This is a bit troublesome, but It is useful for special input, because sscanf () supports regular expressions. For example, if m: 4 is required and the required data is 4, then you can use sscanf () regular expression to ignore: to get the data we have available. (For powerful regular expressions, please refer to this article https://www.cnblogs.com/lanjianhappy/p/7171341.html)
Later, by chance, I tried to mix scanf () and gets (). The difference was that after using getchar () after scanf (), it was found to be possible, probably because getchar () prevented gets () from eating. The carriage return of scanf (). (It seems to be nonsense ...) Then the latter checked it on the Internet. It turned out that this is still possible. Another way is to add scanf ("\ n") after scanf () to add a carriage return. Let's supplement the carriage return eaten by gets ().
That's right, it can be solved simply with scanf (), and it can also be used for C ++ cin. Don't use double gets () to add sscanf () like the author. Of course, it's okay to use it. Special There are situations.
#include <stdio.h>
int main (void)
{
int n;
char a [100];
scanf ("% d", & n); // or so without the next scanf ("\ n"); scanf ("% d \ n", & n);
scanf ("\ n");
gets_s (a);
puts (a);
}