xcode gdb 输出中的 C/Objective-C 中的回车符(“\r”)不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5734740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:51:59  来源:igfitidea点击:

Carriage return ("\r") in C/Objective-C in gdb output not working

objective-ccxcodemacosxcode4

提问by Stephen Searles

I want output to the command line (so far, in the gdb command line within XCode) to overwrite the same line. The code I'm using to print the output right now takes some things from an Objective-C method and eventually does this:

我希望输出到命令行(到目前为止,在 XCode 中的 gdb 命令行中)以覆盖同一行。我现在用来打印输出的代码从一个 Objective-C 方法中获取了一些东西,并最终做到了这一点:

unsigned char _output = ...;
printf("Output:%c\r",_output);
fflush(stdout);

It prints the correct output, but on successive lines instead of overwriting. What could the problem be?

它打印正确的输出,但在连续的行上而不是覆盖。可能是什么问题?

采纳答案by Jeremy W. Sherman

The GDB console in Xcode is not a full-featured terminal emulation. It's intended to support interaction with GDB, and that's it. How control characters are handled is going to depend on the terminal emulator you use. What happens when you run the tool from the commandline directly using Terminal.app? What about xTerm? rxvt?

Xcode 中的 GDB 控制台不是功能齐全的终端仿真。它旨在支持与 GDB 的交互,仅此而已。如何处理控制字符将取决于您使用的终端模拟器。当您直接使用 Terminal.app 从命令行运行该工具时会发生什么?xTerm 呢?是吗?

回答by Thomas Matthews

The interpretation of '\r' and '\n' are up to the platform or terminal. Terminals can be set to append a linefeed (0x0a) to a '\r' automatically. Also, '\n', is newline, which can mean either a single linefeed (assuming a carriage return) or a carriage-return and linefeed pair. For portability, one cannot count on a '\r' to only return the carriage (cursor) to the beginning of the current line.

'\r' 和 '\n' 的解释取决于平台或终端。终端可以设置为自动将换行符 (0x0a) 附加到 '\r'。此外, '\n' 是换行符,这可以表示单个换行符(假设有回车符)或回车符和换行符对。为了可移植性,不能指望 '\r' 仅将回车(光标)返回到当前行的开头。

To get to the beginning of the same line, one may be tempted to use '\b' (backspace). But this is subjective as well. Some output devices interpret the command as destructive, some as moving backward one character, non-destructive and others will ignore it.

为了到达同一行的开头,人们可能会想使用 '\b'(退格)。但这也是主观的。一些输出设备将命令解释为破坏性的,一些解释为向后移动一个字符,非破坏性的,而另一些则会忽略它。

I suggest you use a cursor positioning library instead. Some people recommend NCurses.

我建议您改用光标定位库。有人推荐NCurses