0

My program uses various printf() statements. Is it possible to determine at any point of the program if the stdout cursor is at the start of a new line?

Background: The program uses a custom error routine and I would like to show any error messages on the next blank line. Since the program does not track if the last printf() ended with \n, the error routine currently starts each message by printing \n. This results in two successive \n (a blank line) if the last printf() also ended with \n.

Barmar
  • 741,623
  • 53
  • 500
  • 612
u17
  • 2,776
  • 4
  • 31
  • 43
  • I don't think there's any way to do this. It's up to the application to keep track if it's important. – Barmar Apr 24 '20 at 19:45
  • I can only think of one language that provides a way to do this. Common Lisp has the `FRESH-LINE` function, which outputs a newline unless the last character was a newline. – Barmar Apr 24 '20 at 19:46
  • 1
    For posterity. https://stackoverflow.com/questions/25329670/use-of-fseek-with-stdout – jiveturkey Apr 24 '20 at 19:55
  • The obvious solution would be to write a wrapper function for `printf()` which checks if the format string ends with `\n` and stores that information in a global variable – Felix G Apr 25 '20 at 09:31
  • one easy fix, ALWAYS end each print statement with '\n' – user3629249 Apr 25 '20 at 18:37

1 Answers1

0

Please read linux-cursor position AND windows-cursor position

user3629249
  • 16,402
  • 1
  • 16
  • 17