gpt4 book ai didi

c - 如何删除C中上面几行的书面文字?

转载 作者:行者123 更新时间:2023-12-04 16:29:43 26 4
gpt4 key购买 nike

我正在用 C 编程,对此感到疑惑。假设有这个简单的程序:

Username:
Password:

现在...它看起来很简单,打印起来也很简单 (printf("Username:\nPassword: ");)但如何获取用户名字符串? fgets 当然,但是......我希望用户在“用户名:”之后而不是在密码之后键入它。您可以执行 printf("\b"); 以删除以前写入的数据在同一行 但我怎样才能到达“用户名:”? "\b"在删除"Password:"后就没用了,显然回车也一样。我该怎么做?

最佳答案

如果这是 Windows,您可以像这样移动光标:

#include <stdio.h>
#include <windows.h>

void setCursorPos(int x, int y)
{
HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
SetConsoleCursorPosition(hStdout, csbiInfo.dwCursorPosition);
}

int _tmain(int argc, _TCHAR* argv[])
{
system("cls");
char user[128], pass[128];
printf("Username:\r\nPassword:\r\n");
setCursorPos(10, 0);
fgets(user, 128, stdin);
setCursorPos(10, 1);
fgets(pass, 128, stdin);
printf("User = %s, Pass = %s\r\n", user, pass);
return 0;
}

关于c - 如何删除C中上面几行的书面文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661866/

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