控制台程序,让光标复位的问题?

恨天低 2011-05-23 12:08:09
printf后,怎么让光标回到起始位置?

注:printf多行的!
...全文
119 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Vonvnie 2011-06-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 whitebird99 的回复:]
清屏之后就回到起始位置了....

C/C++ code

system("cls");


好吧,这个方法很不适用...
[/Quote]

这个好像是清屏吧?和光标复位有什么关系?
秃头披风侠 2011-05-23
  • 打赏
  • 举报
回复
清屏之后就回到起始位置了....

system("cls");

好吧,这个方法很不适用...
恨天低 2011-05-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jernymy 的回复:]

使用printf("\b");回退

C/C++ code

printf("\b\b\b");
[/Quote]

不是吧??我print出来的文本很多~。一直退格?!!
jernymy 2011-05-23
  • 打赏
  • 举报
回复
使用printf("\b");回退


printf("\b\b\b");
恨天低 2011-05-23
  • 打赏
  • 举报
回复
比较急,沙发自己坐!
赵4老师 2011-05-23
  • 打赏
  • 举报
回复
方法一:cprintf("%s","12345");cprintf("\r%80s\r%s"," ","abc");
方法二:
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);

int main(int argc, char* argv[])
{
HideTheCursor();
ClearConsoleToColors(15, 1);
ClearConsole();
gotoXY(1, 1);
SetColor(14);
printf("This is a test...\n");
Sleep(500);
ShowTheCursor();
SetColorAndBackground(15, 12);
ConPrint("This is also a test...\n", 23);
SetColorAndBackground(1, 7);
ConPrintAt(22, 15, "This is also a test...\n", 23);
gotoXY(0, 24);
SetColorAndBackground(7, 1);
return 0;
}

//This will clear the console while setting the forground and
//background colors.
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
//not used but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}

//This will clear the console.
void ClearConsole()
{
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}

//This will set the position of the cursor
void gotoXY(int x, int y)
{
//Initialize the coordinates
COORD coord = {x, y};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
}

//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

//Direct console output
void ConPrint(char *CharBuffer, int len)
{
DWORD count;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
}

//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
DWORD count;
COORD coord = {x, y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, coord);
WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}

//Hides the console cursor
void HideTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = FALSE;
}
}

//Shows the console cursor
void ShowTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = TRUE;
}
}

Microsoft Windows 系统错误代码简单分析:   0000 操作已成功完成。   0001 错误的函数。   0002 系统找不到指定的文件。   0003 系统找不到指定的路径。   0004 系统无法打开文件。   0005 拒绝访问。   0006 句柄无效。   0007 存储区控制块已损坏。   0008 可用的存储区不足, 无法执行该命令。   0009 存储区控制块地址无效。   0010 环境错误。   0011 试图使用不正确的格式加载程序。   0012 访问代码无效。   0013 数据无效。   0014 可用的存储区不足,无法完成该操作。   0015 系统找不到指定的驱动器。   0016 无法删除该目录。   0017 系统无法将文件移到其他磁盘驱动器上。   0018 没有其他文件。   0019 媒体写保护。   0020 系统找不到指定的设备。   0021 设备尚未准备好。   0022 设备无法识别该命令。   0023 数据错误(循环冗余检查)。   0024 程序发出命令,但是该命令的长度错误。   0025 驱动器在磁盘上无法定位指定的区域或磁道。   0026 无法访问指定的磁盘或软盘。   0027 驱动器找不到所请求的扇区。   0028 打印机缺纸。   0029 系统无法写入指定的设备。   0030 系统无法读取指定的设备。   0031 与系统连接的设备不能正常运转。   0032 其他进程正使用该文件,因此现在无法访问。   0033 另一进程已锁定该文件的某一部分,因此现在无法访问。   0034 驱动器中的软盘不正确。请将 %2 (卷标序列号: %3)插入驱动器 %1。   0036 打开共享的文件太多。   0038 已到达文件结尾。   0039 磁盘已满。   0050 不支持此网络请求。   0051 远程计算机无法使用。   0052 网络中存在重名。   0053 找不到网络路径。   0054 网络正忙。   0055 指定的网络资源或设备已不可用。   0056 已经达到网络命令的极限。   0057 网络适配器出现错误。   0058 指定的服务器无法执行所请求的操作。   0059 网络出现意外错误。   0060 远程适配器不兼容。   0061 打印机队列已满。   0062 服务器上没有存储等待打印的文件的空间。   0063 已经删除等候打印的文件。   0064 指定的网络名无法使用。   0065 拒绝访问网络。   0066 网络资源类型错误。   0067 找不到网络名。   0068 已超过本地计算机网络适配器卡的名称极限。   0069 已超过网络 BIOS 会话的极限。   0070 远程服务器已经暂停或者正在启动过程中。   0071 由于该计算机的连接数目已达到上限,此时无法再连接到该远程计算机。   0072 指定的打印机或磁盘设备已经暂停。   0080 该文件存在。   0082 无法创建该目录或文件。   0083 INT 24 失败。   0084 处理该请求的存储区不可用。   0085 正在使用该本地设备名。   0086 指定的网络密码不正确。   0087 参数错误。   0088 网络出现写入错误。   0089 此时系统无法启动其他进程。 0100 无法创建其他系统标志。   0101 属于其他进程的专用标志。   0102 标志已经设置, 无法关闭。   0103 无法再次设置该标志。   0104 中断时无法请求专用标志。   0105 此标志先前的所有权已终止。   0106 请将软盘插入驱动器 %1。   0107 后续软盘尚未插入,程序停止。   0108 磁盘正在使用或已由其他进程锁定。   0109 管道已经结束。   0110 系统无法打开指定的设备或文件。   0111 文件名太长。   0112 磁盘空间不足。   0113 没有其他可用的内部文件标识符。   0114 目标内部文件标识符不正确。   0117 该应用程序所运行的 IOCTL 调用

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧