cout-显示的宽度

发布于 2024-12-28 11:57:04 字数 158 浏览 5 评论 0原文

我想知道:是否可以在标准输出中获取行的宽度?不是 std::cout.width() 或 std::setw() 宽度,而是操作系统换行之前的实际最大字符数?

编辑:只是通知一下,我目前正在使用Windows XP,尽管我真的更喜欢以便携式方式来执行此操作(其他一切都是便携式ATM)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海风掠过北极光 2025-01-04 11:57:04

不使用标准 C++。 cout 不必绑定到控制台,其中“宽度”将毫无意义。

当然,也可以使用其他一些库,例如ncurses

如果您使用的是 Linux,请检查在 C 中获取终端宽度?;如果您使用的是 Windows,请检查获取 Windows 的 c 终端大小?。

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?.

日记撕了你也走了 2025-01-04 11:57:04

在我使用的系统(Windows 和 Unix)上,操作系统从不换行。
然而,各种显示设备可能;通常的 xterm 窗口
例如,X 或 Windows 下的控制台窗口将在
一定数量的字符,如果您要输出到其中之一,
有一种依赖于实现的方法来确定大小
片刻。不过我不知道它是否真的有用,因为它
可以在您阅读它的那一刻和您输出它的那一刻之间发生变化。

当输出到文件时,当然,没有换行,我已经
有时需要处理行长度远超过的文件
万个字符。

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.

梦醒时光 2025-01-04 11:57:04

Windows 的控制台宽度

在 Windows 上,使用 windows.h 您可以查询控制台大小并计算宽度,如下所示。

示例 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";

它在 consoleapi2.h 中定义。我不知道遗产状况。在Win11上测试..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文