在 C 中获取控制台缓冲区内容
只是想知道是否有任何方法(在 C 中)获取控制台缓冲区的内容,最好是某种字符数组。 它将被写入一个文件,所以如果我错过了一些可以做到这一点的愚蠢的事情,那么请指出来。 它可以是 Windows 特定的。 我正在使用 MinGW(gcc 3.4.5)。
提前致谢。
Just wondering if there is any way (in C) to get the contents of the console buffer, preferably as some kind of char array. It is going to be written to a file, so if I am missing out on something stupid that will do exactly that, then point it out. It can be Windows-specific. I am using MinGW (gcc 3.4.5).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你需要看看 win32 控制台函数,例如 GetStdHandle()、ReadConsoleOutput()< /a>、WriteConsoleOutput 等。
I think you need to take a look at win32 console functions for example GetStdHandle(), ReadConsoleOutput(), WriteConsoleOutput etc.
在 Win32 中,ReadConsoleOutput:
In Win32, ReadConsoleOutput:
这是读取控制台缓冲区的方法。
GetNumCharsInConsoleBuffer
是获取控制台屏幕缓冲区中写入的总字符数,我使用 GetConsoleScreenBufferInfo 内。 之后,我创建一个动态分配的数组,其大小等于GetNumCharsInConsoleBuffer
返回的先前值 + 1(用于零结尾)。 最后,ReadConsoleBuffer 将使用控制台屏幕缓冲区的内容填充刚刚创建的缓冲区。之后,如果您想将缓冲区的内容写入文件,您可能需要进行一些格式化。 使用 ReadConsoleOutputCharacter,您获取控制台屏幕缓冲区的一个区域(矩形)。 您写入控制台屏幕缓冲区的行将用空格填充以适合缓冲区区域。 与win32
ReadConsoleOutput/WriteConsoleOutput
相同,您将获得屏幕的一个区域(矩形)。输出:
编辑:
我刚刚编写了一个函数,可用于将屏幕缓冲区的内容写入文件。 此函数从屏幕控制台缓冲区中删除填充的空格。
ReadConsoleBufferForFile
采用空字符指针作为第一个参数 (buffer
),该参数将在执行期间分配。 所以不要忘记自己删除它。ReadConsoleBufferForFile
将在第二个参数 (bufsize
) 中写入缓冲区的大小。“out.txt”中的输出:
Here's a way to read the console buffer.
GetNumCharsInConsoleBuffer
is to get the total characters written in the console screen buffer, I use GetConsoleScreenBufferInfo inside. After, I create a dynamically allocated array of size equal to the previously value returned byGetNumCharsInConsoleBuffer
+ 1 (for zero-ending). And finally,ReadConsoleBuffer
will fill the buffer just created with the contents of the console screen buffer.After, if you want to write the contents of your buffer to a file, you'll probably need to do some formatting. With ReadConsoleOutputCharacter you get a region(rectangle) of the console screen buffer. The lines you wrote to the console screen buffer will be padded with spaces to fit the region of the buffer. it'll be the same with the win32
ReadConsoleOutput/WriteConsoleOutput
, you'll get a region(rectangle) of your screen.Output:
EDIT:
I just wrote a function that can be used to write the contents of the screen buffer to file. This function removes the padded spaces from the screen console buffer.
ReadConsoleBufferForFile
takes a null char pointer as first argument (buffer
) that will be allocated during its execution. So don't forget to delete it by yourself.ReadConsoleBufferForFile
will write the size of the buffer in the second argument (bufsize
).Output in 'out.txt':
下载 mingw-readline
并查看 GNU Readline
Download mingw-readline
And look GNU Readline
如果它是命令窗口中的屏幕,那么可能没有,或者没有简单的方法。 您可以破解 Windows 事件来全选、复制和读取剪贴板。
If it's the screen in a command window, then potentially no, or no easy way. You could hack at Windows events to select-all, copy, and read the clipboard.