【问题标题】:printf creates no or wrong outputprintf 不产生或产生错误的输出
【发布时间】:2016-02-01 07:55:25
【问题描述】:

我编写了一个程序,出于调试目的,我想在控制台中写入一些文本。现在我发现了一个非常奇怪的错误并且找不到解决方案。这是我的代码:

int main(void)
{
    setvbuf (stdout, NULL, _IONBF, 0);
    char input[50];
    char check = 'a';
    for(int i=0; check != EOF; ++i){
        check = scanf("%s", input);
        printf("%s\n",input);
    }

    fflush(stdout);

    char* myAnswer = createList();
    printf("%s\n", myAnswer);

    return 0;
}
//-----------------------------------------------------------------------------

char* createList(){
    char* msg = malloc(6*sizeof(char));
    msg[0]='A';
    msg[1]='B';
    msg[2]='C';
    msg[3]='D';
    msg[4]='E';
    msg[5]='\0';
    return msg;
}

for 循环工作正常,但从未编写过“ABCDE”。相反,有时我在输入中保存的最后一个单词会第二次写入控制台,缺少最后一个字母。或者根本什么都没有写。 我试图通过刷新缓冲区或将其设置为零大小来解决它。但没有任何帮助。我使用 Qt Creator,错误可能出在我的 IDE 中吗?

【问题讨论】:

  • scanf() 返回一个int。不是char
  • 谢谢,我改了。但不影响输出
  • 它对我来说工作正常。为你的函数添加一个原型。
  • @Jonas 停止检查 EOF ,检查 scanf 是否返回 1 。如果不是,那么 scanf 失败。
  • @Haris 恐怕不行。这不是 scanf 返回的内容。它返回成功匹配的参数数量 -cplusplus.com/reference/cstdio/scanf

标签: c stream printf buffer console-application


【解决方案1】:

更正了代码的某些部分(例如在 EOF 上中断循环、将数据类型更改为 int 等)。请查看以下代码是否有效。需要在最后一次输入后按 Ctrl-D 以确保循环中断。

#include <stdio.h>
#include <stdlib.h>

char* createList();

int main(void)
{
    setvbuf (stdout, NULL, _IONBF, 0);
    char input[50];
    for(;;){
        if (fgets(input, 50, stdin) == NULL)
            break;
        printf("%s\n",input);
    }

    fflush(stdout);

    char* myAnswer = createList();
    printf("%s\n", myAnswer);

    return 0;
}
//-----------------------------------------------------------------------------

char* createList(){
    char* msg = (char *) malloc(6*sizeof(char));
    msg[0]='A';
    msg[1]='B';
    msg[2]='C';
    msg[3]='D';
    msg[4]='E';
    msg[5]='\0';
    return msg;
}

【讨论】:

  • strg+D 只是在控制台中写入一个 '^D'
  • 这个答案没有解决所有的错误。用户可能会超出输入缓冲区(因为scanf() 的格式字符串上没有最大长度修饰符。)这会导致未定义的行为,并可能导致段错误事件。
  • 是的,没错。刚刚尝试修复报告的问题。根据您的反馈,我正在编辑代码以将 scanf 替换为 fgets,
【解决方案2】:

以下代码

1) eliminates the code clutter
2) checks for errors
3) compiles cleanly
4) when user input <CTRL-D> (linux) then program receives a EOF

#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT_LEN (50)

char *createList(void);

int main(void)
{
    char input[MAX_INPUT_LEN];


    while(  1 == scanf("%49s", input) )
    {
        printf("%s\n",input);
    }


    char* myAnswer = createList();
    printf("%s\n", myAnswer);

    return 0;
}
//-----------------------------------------------------------------------------

char* createList(){
    char* msg = malloc(6);
    if( NULL == msg )
    { // then malloc failed
        perror( "malloc failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    msg[0]='A';
    msg[1]='B';
    msg[2]='C';
    msg[3]='D';
    msg[4]='E';
    msg[5]='\0';
    return msg;
}

【讨论】:

    猜你喜欢
    • 2017-01-03
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多