Win32 API 控制台光标在 WriteConsole 后不移动
Win32 API console cursor not moving after WriteConsole
所以,当我尝试使用此代码从控制台读取 std::wstring 时
std::wstring string;
wchar_t c;
DWORD u;
do {
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &u, NULL);
} while (u && (c == L' ' || c == L'n'));
do {
string.append(1, c);
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &u, NULL);
} while (u && c != L' ' && c != L'n');
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), string.data(), string.length(), &u, NULL);
写入字符串后,光标位置不会移动,因此如果我再次调用 WriteConsole((,它将写入刚刚写入的字符串上方。 有什么解决方案吗?
当ReadConsole
读取 stdin时,它会将rn
附加到字符串中。您只排除了条件中的n
。"stringr"
将被传递给WriteConsole
,r
会将光标返回到行首。 请尝试以下代码:
#include <windows.h>
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
std::wstring string;
wchar_t c;
DWORD u;
do {
ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &u, NULL);
} while (u && (c == L' ' || c == L'n'));
do {
string.append(1, c);
ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &u, NULL);
} while (u && c != L' ' && c != L'n' && c != L'r');
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string.data(), string.length(), &u, NULL);
return 0;
}
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- 将std::string传递给WriteConsole API
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- SendInput()鼠标移动计算
- 按值 C++ 返回时进行双倍移动
- 移动二维数组中的字符
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- Win32 API 控制台光标在 WriteConsole 后不移动