简体   繁体   English

Visual C ++ Win32控制台应用程序printf在while循环内的多行输出

[英]Visual C++ Win32 Console application printf output on multiple lines within while loop

I am trying to display data on multiple lines in my console application using printf. 我正在尝试使用printf在控制台应用程序中多行显示数据。 The following code displays the data like this: 以下代码显示如下数据:

Default Data=00000000 ACP Status Request=00000000 ACP VHF1 Data=00000000

What I need is: 我需要的是:

Default Data=00000000

ACP Status Request=00000000

ACP VHF1 Data=00000000

When I use \\n the data fills the console screen over and over again. 当我使用\\ n时,数据一遍又一遍地填满控制台屏幕。 Can anyone suggest a solution and explain why the loop is fine and the code works untill I try and goto the next line. 任何人都可以提出解决方案并解释为什么循环很好并且代码可以正常工作,直到我尝试进入下一行为止。 Thanks. 谢谢。

while(!_kbhit())
{
    /*Read message records*/
    msgdefault.data = BTI429_MsgDataRd(msgdefault.addr,hCore);

    STAT_REQ.data = BTI429_MsgDataRd(STAT_REQ.addr,hCore);

    VHF1.data   = BTI429_MsgDataRd(VHF1.addr,hCore);

    /*Display values*/
    printf("\r");
    printf("Default Data=%08lX ",msgdefault.data);
    //printf("\n");
    printf("ACP Status Request=%08lX ",STAT_REQ.data);
    //printf("\n");
    printf("ACP VHF1 Data=%08lX ",VHF1.data);
}

You need some function to jump to the start of your screen. 您需要一些功能才能跳到屏幕的开始。

Read your documentation, there may be some function like gotoxy(0,0) or something else. 阅读您的文档,可能有一些功能,例如gotoxy(0,0)或其他功能。

Try searching for System::Console::SetCursorPosition , this may help you. 尝试搜索System::Console::SetCursorPosition ,这可能会对您有所帮助。

Try adding following code in the start of loop before printing anything 在打印任何内容之前,尝试在循环开始时添加以下代码

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

_COORD p;
p.X = x;
p.Y = y;

SetConsoleCursorPosition(hConsole, p);

But make sure to print some empty spaces after your prints so that the remains of old prints are overwritten. 但是请确保在打印后打印一些空白区域,以便覆盖旧打印的剩余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM