简体   繁体   中英

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 output, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM