为什么我的代码中会出现分段错误? (三)

问题描述 投票:0回答:0

我正在尝试向用户请求三个字符串,虽然前两个可以正常工作并通过,但第三个在运行时会出现分段错误。除了变量名外,前两个与第三个几乎相同,所以我很困惑为什么它只在这里给出错误。

        printf("Input Text File Name (with .txt): \n");
        char text [50];
        scanf("%s", text);

        // Gets query from user.
        printf("Input Query Word: \n");
        char query [50];
        scanf("%s", query);

        // Gets query length
        int length = strlen(query);

        // Gets replacement word from user.
        printf("Input Replacement Word: \n");
printf("WHOOP");
        char replace [50];
printf("WHOOP2");
        scanf("%s", replace);
printf("WHOOP3");
        // For counting the number of Replacements.
        int count = 0;
printf("WHOOP4");

Whoop 和 Whoop2 都打印,但 Whoop 3 和 4 不打印。变量 text 和 query 都通过并正确打印,但 replace 是唯一由于某种原因导致问题的变量。我尝试在每次 scanf 后清除输入缓冲区,但结果是一样的。

我是 c 的新手,所以我可能只是愚蠢。谢谢你的帮助!

c segmentation-fault user-input
© www.soinside.com 2019 - 2024. All rights reserved.