1

I'm very new to coding in general so please excuse any stupid things I say.

I am trying to print (in c) a rather long text using printf() but since it can't all fit on the screen it jumps to the end of the text and the beginning is not visible unless you scroll up. Is there an easy way to have it print the long text but stay at the beginning and allow the user to scroll down as they read before they put in the next command?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85

3 Answers3

2

On Unix (including Linux and Mac), there are command line programs built in called more and less that does exactly what you describe. more is a program that simply waits for the user to press enter or space before showing the next page of output. less is slightly improved in that it allows vi editor keystrokes (such as j and k) to scroll up and down in the output.

more is also available on the Windows command line as well. You might even be able to find a version of less for Windows as well.

c:\users\selbie> your_program.exe | more

$> ./your_program | less

As to how to do this programmatically, that's a bit more difficult as it would involve measuring the console width and implementing your own scroll buffers. There might be open-source libraries that provide this functionality, but the console environment already has a solution for apps that produce long output.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • That sounds like what I need. Thanks a lot friend. – Bill Billerson Aug 25 '18 at 05:11
  • Note that OP said there was a need for the user to "put in the next command", so I'm not sure this solution will work, because it effectively makes the program output-only. – John Zwinck Aug 25 '18 at 05:40
  • uh oh. I think you might be right @JohnZwinck. I Think i found a way to get the current height of the terminal window so I'll just prompt the user to press enter after that many lines. – Bill Billerson Aug 25 '18 at 05:51
  • I have some sample code for getting the sample code on Unix [here](https://github.com/jselbie/stunserver/blob/master/common/getconsolewidth.cpp) and for Windows, you can find an example [here](https://stackoverflow.com/questions/6812224/getting-terminal-size-in-c-for-windows) – selbie Aug 25 '18 at 17:16
0

Not really, though you may find a reasonable and simple solution is to print only a certain number of lines (say 30), then prompt the user to press Enter before display more lines.

You can even find out the current size of the terminal. That's platform specific; for Linux it's explained here: How to get terminal window width?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

Not in a standard way, no.

Your output stream in C is just a stream of characters -- scrolling is handled by your terminal.

Depending on your terminal, it may be possible to control scrolling by outputting special characters, like ANSI escape codes. The ncurses library provides a portable way to manipulate terminals.

However, if you just want a more convenient way to look through your output (or really any command output), @selbie's answer is the best: use more or less. This will avoid any extra complexity in your program.

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32