【问题标题】:Why does not the input of a string work?为什么字符串的输入不起作用?
【发布时间】:2023-03-10 05:05:01
【问题描述】:
#include <stdio.h>
int main()
{
        char temp[1024];
        if(getchar() != 'y')
        {
                printf("no options\n");
                return 1;
        }
        scanf(temp, "%s");
        printf("%s", temp);
}

我得到如下的 sn-p。我只想从用户那里输入两次。但是第一个输入有效,但是第二个直接跳过并且printf("%s", temp); 打印了意外字符。我该如何解决这个问题.. 谢谢

【问题讨论】:

  • 最好保持一致,如果你从main返回任何值,那么从函数末尾也返回一个有效值(通常为0)。
  • 请参考scanf函数的使用方法,传入的参数必须是逆序... scanf( "%s", temp );

标签: c windows linux input


【解决方案1】:

scanf的第一个参数是格式,第二个是缓冲区。你倒过来了。试试scanf( "%s", temp );

【讨论】:

  • 或者,正如我们喜欢说的那样。你有它 bass-ackward :-) 你还应该知道 scanf 不应该用于强大的应用程序中的输入,因为没有办法防止缓冲区溢出(使用 fgets 和 sscanf 作为一种选择)。但是,由于您处于初学者级别,您可以为后者提交该建议(无意侮辱,只是指出您可能还不需要它)。
  • paxdiable:不同意最后一句话。除非安全函数要复杂得多,否则为什么不先教初学者使用安全函数,然后再教危险函数呢?在习惯形成阶段获得它们!
  • @itowlson,如你所愿,我在答案中添加了一个更安全的版本。
【解决方案2】:

需要将参数切换为scanf

#include <cstdio>

using namespace std;

int main()
{
    char buf[100];
    while (true)
    {
        if (scanf("%s",buf) == EOF)
        {
            printf("fail");
            return 1;
        }
    }
}

【讨论】:

    【解决方案3】:

    其他人已经给了你实际的答案,你应该接受其中一个,但如果你喜欢这个明智的建议,请随时给我投票:-)

    如果您正在寻找功能强大的应用程序,则绝不应考虑使用gets。那是因为没有办法防止缓冲区溢出导致程序不安全。

    在下面的程序中,我更喜欢getLine() 这样的小函数。它使用fgets可以防止溢出,是一个强大的解决方案。

    #include <stdio.h>
    #include <string.h>
    
    #define OK       0
    #define NO_INPUT 1
    #define TOO_LONG 2
    static int getLine (char *prmpt, char *buff, size_t sz) {
        int ch, extra;
    
        // Get line with buffer overrun protection.
        if (prmpt != NULL) {
            printf ("%s", prmpt);
            fflush (stdout);
        }
        if (fgets (buff, sz, stdin) == NULL)
            return NO_INPUT;
    
        // If it was too long, there'll be no newline. In that case, we flush
        // to end of line so that excess doesn't affect the next call.
        if (buff[strlen(buff)-1] != '\n') {
            extra = 0;
            while (((ch = getchar()) != '\n') && (ch != EOF))
                extra = 1;
            return (extra == 1) ? TOO_LONG : OK;
        }
    
        // Otherwise remove newline and give string back to caller.
        buff[strlen(buff)-1] = '\0';
        return OK;
    }
    

     

    // Test program for getLine().
    
    int main (void) {
        int rc;
        char buff[10];
    
        rc = getLine ("Enter string> ", buff, sizeof(buff));
        if (rc == NO_INPUT) {
            printf ("No input\n");
            return 1;
        }
    
        if (rc == TOO_LONG) {
            printf ("Input too long\n");
            return 1;
        }
    
        printf ("OK [%s]\n", buff);
    
        return 0;
    }
    

    使用 'hello'、CTRLD 和太大的字符串运行示例:

    pax> ./qq
    Enter string> hello
    OK [hello]
    
    pax> ./qq
    Enter string>
    No input
    
    pax> ./qq
    Enter string> dfgdfgjdjgdfhggh
    Input too long
    
    pax> _
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2012-11-30
      • 1970-01-01
      相关资源
      最近更新 更多