How can I use scanf_s("%[^\n]s")
with a limit to the number of characters allowed? C For example: scanf_s("%10s", ...);
But how can i do this when theres [^\n]
before the %
?
The s
in format "%[^\n]s"
serves no purpose. Delete that s
.
Likely OP wants something like:
char buf[100];
...
// v---- Consume all leading white-space
if (scanf_s(" %[^\n]", buf, XXX sizeofbuf) == 1) {
// ^^^ ^^^^^^^^^^^^^ 2 arguments
Success();
} else {
Fail();
}
where XXX
is either (int)
(Visual) or (rsize_t)
/ nothing per the C standard.
scanf_s()
is not portable to systems that do not implement ..._s
functions.
scanf_s()
may not read only one line as the " "
reads white-space including multiple lines of only white-space.
scanf_s()
is a poor substitute to the preferable fgets()
to read one line of user input. Recommend to never use scanf_s()
nor scanf()
until you know why there are bad.
Alternative:
char buf[100];
if (fgets(buf, sizeof buf, stdin)) {
Success();
} else {
Fail();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.