从控制台读取正好 50 个字符(49 +1 表示 null),仅忽略所有初始空格

发布于 2025-01-13 01:50:03 字数 347 浏览 1 评论 0原文

我如何从控制台中的用户输入中读取恰好 50 个字符(49,+1 表示空),忽略所有初始空格(如第一个之前的那样) word),但之后保留 49 个字符,包括所有空格?

此外,如果用户输入少于 49 个字符并输入换行符,它只会处理该内容(换行符之前的所有内容都会忽略初始空格)。

我尝试使用:

char name[50];
scanf("%49s",name);

但是,这似乎忽略了所有空白;所以,如果我的名字是这样的:“Mark Smith”,它将无法正确处理。

How would I read in exactly 50 characters (49, +1 for null), from a user's input in the console, ignoring only all initial whitespace (as in before the first word) but keeping exactly 49 characters after that, including all whitespace?

Also, if the user were to enter less than 49 characters and enter a newline instead, it would just process that (everything up to the newline ignoring initial whitespace).

I tried to use:

char name[50];
scanf("%49s",name);

However, this seems to ignore all whitespace; so, if my name were to be like: "Mark Smith" it would not process it correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2025-01-20 01:50:03

您可以在输入格式说明符之前添加一个(单个)空格,以跳过任何/所有前导空白字符,然后使用[set] 格式说明符(前面带有“49”限制)以输入与给定“set”匹配的所有后续字符。在您的情况下,该集合可以是除了换行符之外的任何内容,因此在集合之前使用“not”前缀 (^),并使集合包含 >just 换行符 (\n):

#include <stdio.h>

int main()
{
    char name[50];
    printf("Enter input: ");
    int n = scanf(" %49[^\n]", name);
    if (n != 1) {
        printf("Input error!");
    }
    else {
        printf("Input was: %s\n", name);
    }
    return 0;
}

测试:

Enter input:        I am the egg-man; I am the egg-man; I am the walrus!
Input was: I am the egg-man; I am the egg-man; I am the walr

注意,使用上面的代码,如果输入的输入字符串超过 49 个字符,那么多余的字符将被保留在输入缓冲区。这些很可能会导致进一步的问题,因为它们将被用作对 scanf (或类似输入函数)的任何后续调用的输入。

有几种方法可以“清除”输入缓冲区:How to Clear input buffer in C? 简要总结一下链接问题的答案:

  1. 从不使用(或尝试使用)fflush(stdin)
  2. 以下这样的代码是典型的方法:
int c;
while ( (c = getchar()) != '\n' && c != EOF ) { }

You can precede the input format specifier with a (single) space, to skip any/all leading whitespace characters and then use the [set] format specifier (with a preceding "49" limit) to input all subsequent characters that match the given "set". In your case, that set would be anything except a newline character, so use the "not" prefix (^) before the set, and make the set consist of just the newline (\n):

#include <stdio.h>

int main()
{
    char name[50];
    printf("Enter input: ");
    int n = scanf(" %49[^\n]", name);
    if (n != 1) {
        printf("Input error!");
    }
    else {
        printf("Input was: %s\n", name);
    }
    return 0;
}

Test:

Enter input:        I am the egg-man; I am the egg-man; I am the walrus!
Input was: I am the egg-man; I am the egg-man; I am the walr

Note that, using the above code, if an input string of more than 49 characters is entered, then the excess characters will be left in the input buffer. These will very likely cause problems further down the line, as they will be taken as input to any subsequent calls to scanf (or similar input functions).

There are several ways to 'clear' the input buffer: How to clear input buffer in C? To give a brief summary of the answers to the linked question:

  1. Never use (or attempt to use) fflush(stdin).
  2. Code like the following is a typical way:
int c;
while ( (c = getchar()) != '\n' && c != EOF ) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文