cout-显示的宽度
我想知道:是否可以在标准输出中获取行的宽度?不是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不使用标准 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?.
在我使用的系统(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 underX 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.
Windows 的控制台宽度
在 Windows 上,使用
windows.h
您可以查询控制台大小并计算宽度,如下所示。示例 c++
它在
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++
It is defined in
consoleapi2.h
. I don't know the legacy status. Tested on Win11..