0

I am writing a program in C and my question is pretty straightforward: How to know if my next call to printf() will exceed screen size?

That is, sometimes when you use print, the text may not fit in one screen. E.g. I have 4-5 strings to print and I would like to create a function that prints the entire text or prints until the text is cut off due to the screen size.

Alex
  • 358
  • 1
  • 11
  • You could `sprintf` into a buffer, get the length of that with `strlen`, keep track of the total in a counter, and output the buffer with `printf`. – 500 - Internal Server Error May 29 '20 at 11:38
  • Count characters before printing? Use `sprintf` to print to a string buffer, then put to console, inserting `\n` when width is exceeded? – Paul Ogilvie May 29 '20 at 11:38
  • 1
    @500-InternalServerError You do not need `strlen` for that, `sprintf` returns the number of characters written to the buffer. – mch May 29 '20 at 11:40
  • @mch: Ah, good call - I forgot. – 500 - Internal Server Error May 29 '20 at 11:40
  • I would expect to exist a system function which is triggered when the monitor scrolls down in order to "follow" the printed output. That is, when somebody keeps printing in the terminal, the terminal automatically scrolls down so you can monitor the printed output. Does such a thing exist? For your previous comments, we don't know in advance how many characters fit in the screen, this is platform dependent right? – Alex May 30 '20 at 09:42

2 Answers2

1

You can use snprintf to write into a string buffer and then write this buffer to the actual console output. Since it seems that you cannot control the number of characters you want to write, I suggest to use int snprintf( char* buffer, std::size_t buf_size, const char* format, ... ). It allows you to limit the number of characters to be written to avoid buffer overflows:

char buffer[80+1];  // maximum length of a complete line.
snprintf(buffer, sizeof(buffer), "my output: %d, %s", someInt, someString);
if (strlen(buffer) >= nrOfCharsLeftInLine) {
   buffer[nrOfCharsLeftInLine] = 0x0;
}
printf("%s",buffer);
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Hmm so there is not any system function for this? How do we know how many characters fit in a screen? – Alex May 30 '20 at 09:38
  • I would expect to exist a system function which is triggered when the monitor scrolls down in order to "follow" the printed output. That is, when somebody keeps printing in the terminal, the terminal automatically scrolls down so you can monitor the printed output. Does such a thing exist? For your previous comments, we don't know in advance how many characters fit in the screen, this is platform dependent right? – Alex Jun 09 '20 at 17:20
0

According to How do you clear the console screen in C?, "C doesn't understand the concept of screen. So any code would fail to be portable". So to make my question more specific, I'm looking for a way to perform the above in a UNIX environment.

I rephrase my question:

Sometimes when you use print, the text may not fit in one screen. E.g. I have 4-5 strings to print and I would like to create a function that prints until the text is cut off because it cannot fit in the available screen size left.

I would expect to exist a system function (of UNIX system) which is triggered when the monitor scrolls down in order to "follow" the printed output. That is, when somebody keeps printing in the terminal, the terminal automatically scrolls down so you can monitor the printed output. Does such a thing exist?

Alex
  • 358
  • 1
  • 11