开发者

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

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:

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

It prints the correct ou开发者_如何学编程tput, but on successive lines instead of overwriting. What could the problem be?


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?


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.

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜