gpt4 book ai didi

c - stdout 上的 freopen_s 导致 Windows 上的 GetConsoleScreenBufferInfo 出现问题

转载 作者:可可西里 更新时间:2023-11-01 10:02:00 25 4
gpt4 key购买 nike

要临时将 stdout 重定向到一个文件,我正在做:

printf("Before");
freopen_s(&stream, "test.txt", "w", stdout);
printf("During");
freopen_s(&stream, "CONOUT$", "w", stdout);
printf("After");

这行得通,但是这样做:

CONSOLE_SCREEN_BUFFER_INFO sbi = {0};
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbi);

似乎不再有效。它返回 false,GetLastError() 返回 6,这会转换为消息“句柄无效”。使用 FormatMessage

关于为什么句柄可能无效的任何建议?有趣的是,printf 继续按预期工作并且 SetConsoleTextAttribute 甚至可以使用相同的句柄。

最佳答案

我发现了以下 here :

hConsoleOutput [in]

A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.

因此,我希望添加对重新打开的读取访问权限将恢复预期的功能,例如:

printf("Before");
freopen_s(&stream, "test.txt", "w", stdout);
printf("During");
freopen_s(&stream, "CONOUT$", "w+", stdout);
printf("After");

关于c - stdout 上的 freopen_s 导致 Windows 上的 GetConsoleScreenBufferInfo 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1691285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com