3

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?

Stephen Searles
  • 971
  • 6
  • 18
  • You can try `freopen(NULL, "ab", stdout);` before the outputs. Maybe your library implementation is interpreting the `'\r'` as `"\r\n"` and switching to binary mode makes it do the right thing. – pmg Apr 20 '11 at 18:34
  • Yeah. I think it has something to do with `'\r'` also meaning `'\n'`, but `freopen` didn't do it. I'll have to take some more time to try mu's suggestion. – Stephen Searles Apr 20 '11 at 19:04
  • Just another update, trying things, `printf("Output\rTheOutput:%c\r",_output);` prints "Output" and "TheOutput" on separate lines. Interpreting `'\r'` as `"\r\n"` seems even more likely. – Stephen Searles Apr 20 '11 at 19:32

2 Answers2

3

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?

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
  • Yeah, I finally got it running in Terminal (had other issues that are now fixed), and it works properly there. Do you know if there's a way to make the GDB console work as I want it to? Or change how XCode debugging works? – Stephen Searles Apr 20 '11 at 22:33
  • The Xcode 3 settings for the Active Executable included some configuration options about the kind of console that Xcode supplied. I don't see those options in Xcode 4, but that could be because I'm not looking in the right place. I also doubt any of them would make it behave as you want it to. – Jeremy W. Sherman Apr 20 '11 at 22:43
1

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.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154