[英]Printing to stderr using fprintf in C
I am writing a C program that takes in a text file describing a game of Solitaire.我正在编写一个 C 程序,该程序接收一个描述纸牌游戏的文本文件。 The program will then take in a set of moves, described by the user in the text file, and go through the moves, modifying the state of the game.
然后程序将采用用户在文本文件中描述的一组动作,并通过这些动作 go 修改游戏的 state。
Currently, I am processing the moves, if there is an invalid move, I stop a while loop, and print to standard error the move in the format "Move M is illegal: (move)".目前,我正在处理移动,如果有无效移动,我会停止一个 while 循环,并以“移动 M 是非法的:(移动)”格式将移动打印到标准错误。
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++;
}
The above is in my main method.以上是我的主要方法。 printString will be given below, it simply prints the given string.
printString 将在下面给出,它只是打印给定的字符串。
processMove, takes the stacks of cards and process a move, it will return -1 if the move is invalid, and -2 if the move has a formatting error. processMove,获取卡片堆栈并处理移动,如果移动无效,它将返回-1,如果移动有格式错误,它将返回-2。
printMove, takes a rank, and suit, and a string, and writes the error to the given string, this would be printed in the fprintf statement. printMove,获取等级、花色和字符串,并将错误写入给定字符串,这将在 fprintf 语句中打印。
After running the above code, I am left with this output, you can see the first call of printString(errString), followed by the second call, after errString has been modified by printMove function.运行上面的代码后,我留下了这个output,可以看到第一次调用printString(errString),接着是第二次调用,errString被printMove修改后function。 Then finally you see the printf statement, which prints the value of processedMove and errString.
然后最后你会看到 printf 语句,它打印出 processesMove 和 errString 的值。
Commencing the printing of the string with indices
c[0]: h
c[1]: o
c[2]:
Commencing the printing of the string on one line
ho
Commencing the printing of the string with indices
c[0]: 5
c[1]: -
c[2]: >
c[3]: 2
Commencing the printing of the string on one line
5->2
6 5->2
the function printString function 打印字符串
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");
}
and the function printMove和 function printMove
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;
}
Thank you for your time, and any solution is welcome.感谢您抽出宝贵时间,欢迎任何解决方案。
So the problem here is, that it you cannot use your own variables in a stream to stderr.所以这里的问题是,你不能在 stream 到 stderr 中使用自己的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.