在C中使用fprintf打印到stderr

在C中使用fprintf打印到stderr,c,C,我正在写一个C程序,它接收一个描述纸牌游戏的文本文件。然后,程序将接收一组由用户在文本文件中描述的动作,并执行这些动作,修改游戏状态 目前,我正在处理移动,如果有无效的移动,我会停止while循环,并以标准错误的格式打印move M是非法的:move char* errString = malloc(sizeof(char) * 10); errString[9] = '\0'; printString(errString); int processedMove = 0; int somethi

我正在写一个C程序,它接收一个描述纸牌游戏的文本文件。然后,程序将接收一组由用户在文本文件中描述的动作,并执行这些动作,修改游戏状态

目前,我正在处理移动,如果有无效的移动,我会停止while循环,并以标准错误的格式打印move M是非法的:move

char* errString = malloc(sizeof(char) * 10);
errString[9] = '\0';
printString(errString);
int processedMove = 0;
int somethingWrong = 1;


while (processedMove < movesStack.size) {

    somethingWrong = processMove(movesStack.cards[processedMove].rank, movesStack.cards[processedMove].suit, &clubsFoundationStack, &diamondsFoundationStack, &heartsFoundationStack, &spadesFoundationStack, &colSevenDown, &colSixDown, &colFiveDown, &colThreeDown, &colFourDown, &colTwoDown, &colOneDown, &colSevenUp, &colSixUp, &colFiveUp, &colThreeUp, &colFourUp, &colTwoUp, &colOneUp, &stockDown, &stockUp, &limitValue, &turnValue);

    if (somethingWrong != 1) {


        printMove(movesStack.cards[processedMove].rank, movesStack.cards[processedMove].suit, &errString);
        printString(errString);
        processedMove++;
        printf("%d %s\n",processedMove, errString);
        fprintf(stderr, "Move %d is invalid %s\n", processed Move, errString);

        break;
    }
    processedMove++;
}
函数printString

void printString(char* c) {
    if (c == NULL) {
        printf("string is null\n");
        return;
    }
    printf("\nCommencing the printing of the string with indices\n");
    for (int i = 0; i < strlen(c); i++) {
        if (c[i] == '\n') {
            printf("        c[%d]: newline\n", i);
            continue;
        }

        printf("        c[%d]: %c\n", i, c[i]);
    }

    printf("Commencing the printing of the string on one line \n");
    printf("        ");
    for (int i = 0; i < strlen(c); i++) {
        if (c[i] == '\n' || c[i] == ' ') {
            continue;
        }
        printf("%c", c[i]);
    }
    printf("\n");


}

感谢您的时间,欢迎您提供任何解决方案。

因此这里的问题是,您不能在stream to stderr中使用自己的变量

您显示的代码无法生成。请把你的问题包括在内。至于你的问题,你是如何运行你的程序的?在什么环境下?如果您在IDE中运行,可能它正在将stderr重定向到其他地方?我使用的是Visual Studio,我在cmd提示符下编译它,然后通过命令提示符运行可执行文件,看起来您正在输出三次,1 PrintStringerString;然后是2个printf%d%s\n,processedMove,errString;和fprintfstderr,移动%d无效%s\n,processedMove,errString;?这是有意的吗?printMove函数可能会写越界,如果没有看到MTR,就不能说你自己的变量是什么意思?你是怎么解决的?工作代码是什么样子的?你的答案基于哪些文件?请阅读。
void printString(char* c) {
    if (c == NULL) {
        printf("string is null\n");
        return;
    }
    printf("\nCommencing the printing of the string with indices\n");
    for (int i = 0; i < strlen(c); i++) {
        if (c[i] == '\n') {
            printf("        c[%d]: newline\n", i);
            continue;
        }

        printf("        c[%d]: %c\n", i, c[i]);
    }

    printf("Commencing the printing of the string on one line \n");
    printf("        ");
    for (int i = 0; i < strlen(c); i++) {
        if (c[i] == '\n' || c[i] == ' ') {
            continue;
        }
        printf("%c", c[i]);
    }
    printf("\n");


}
void printMove(char f, char s, char** errString) {
    char* ret = malloc(sizeof(char) * strlen(*errString));
    if (f == '.' && s == '.') {
        ret[0] = '.';
        ret[1] = '\0';

    }
    else if (f == 'r' && s == 'r') {
        ret[0] = 'r';
        ret[1] = '\0';

    }
    else {
        ret[0] = f;
        ret[1] = '-';
        ret[2] = '>';
        ret[3] = s;
        ret[4] = '\0';
    }

    *errString = ret;
}