1

I'm wondering: is it possible to get the width of a line in the standard output? Not the std::cout.width() or std::setw() widths, but the actual maximum number of characters before the OS will wrap a line?

EDIT: just to inform, I'm currently using windows xp, though I'd really prefer a portable manner to do this (everything else is portable atm).

paul23
  • 8,799
  • 12
  • 66
  • 149

3 Answers3

5

Not using standard C++. cout does not necessary be bound to a console, in which the "width" would be meaningless.

Of course it is possible using some other libraries, e.g. ncurses.

If you're using Linux, check Getting terminal width in C?; if you're using Windows, check Getting terminal size in c for windows?.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
3

On the systems I use (Windows and Unix), the OS never wraps a line. Various display devices might, however; the usual xterm windows under X or a consol window under Windows, for example, will wrap after a certain number of characters, if you're outputting to one of these, there is an implementation dependent way of determining the size at the moment. Whether it is really useful, however, I don't know, since it can change between the moment you read it and the moment you output.

When outputting to a file, of course, there is no wrap, and I've occasionally had to deal with files with a line length of well over a million characters.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • @paul32: CMD.EXE > Properties > Screen Buffer Width > 201. No wrap. The worst part, it means that even if you could determine how wide `std::cout` is now, it might change before you write to it. – MSalters Jan 23 '12 at 14:49
  • @paul23 It doesn't with the two implementations I have at my disposal (g++ 4.4.2 and VS 2010). I don't think the standard even allows wrapping. The output device may wrap the display, but that has nothing to do with the program---write to a file, then look at what's in the file. – James Kanze Jan 23 '12 at 16:37
  • No-op edit to remove downvote. (Sorry -- didn't realize you were speaking about the "program itself" rather than the actual output) – Billy ONeal Jan 23 '12 at 17:31
  • @BillyONeal I'm speaking about the output of the program. I'm not sure what you mean by the "actual output"---the output is a sequence of bytes. And there is no wrapping in this sequence of bytes. Any wrapping is an artifact of the display device: a characteristic of how the display device displays the data, and not of the data (output) itself. It has nothing to do with the program which creates the data, nor with the OS. – James Kanze Jan 24 '12 at 10:54
0

Console width for Windows

On windows using windows.h you can query for the console size and calculate the width as follows.

Example c++

#include <iostream>
#include <windows.h>

CONSOLE_SCREEN_BUFFER_INFOEX consolesize{};
consolesize.cbSize = sizeof(consolesize);
GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE) ,&consolesize);

// calculate width
SHORT width = consolesize.srWindow.Right - consolesize.srWindow.Left;

// Fill line with *
std::cout << std::setfill('*') << std::setw(static_cast <int>(width)) << "\n";

It is defined in consoleapi2.h. I don't know the legacy status. Tested on Win11..

Blindman67
  • 51,134
  • 11
  • 73
  • 136