MFC程序中打开了一个控制台窗口,第二次打开的时候如何清空第一次打开窗口的所有内容

图片说明
MFC程序中打开了一个控制台窗口,执行了一些输入输出的功能。关闭这个窗口后下次再打开这个窗口依然会保留着上次控制台窗口的内容。
程序中通过点击按钮第二次打开这个窗口结果还是上一次执行的结果,怎么样才能清空上一次窗口的内容。
void CC350UdpTestDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
LoadDouble load;
load.length = 0;
char sendBuffer[1000];

memset(&load.container, 0, 100);

AllocConsole();
SetConsoleTitle(_T("Input Double Data"));
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);

cout << "Input:" << endl;

int i = 0;
while (cin >> load.container[i])
{
    load.length++;
    cout << load.container[i] << " ";
    i++;
    if (i > 100)
        break;
}

memset(sendBuffer, 0, 1000);
memcpy(sendBuffer, &load, sizeof(load));
sendBuffer[sizeof(load)] = 0x00;

LoadDouble loadtest;
memset(&load.container, 0, 100);
memcpy(&loadtest, sendBuffer, sizeof(loadtest));
cout << "loadtest.length:" << loadtest.length << endl;

sendto(sockClient, sendBuffer, sizeof(sendBuffer), 0, (SOCKADDR*)&local, len);

cout << "Successfully Send!" << endl;

system("pause");
fclose(stdout);
fclose(stdin);
int ret = FreeConsole();
if (ret == 0)
{
    MessageBox(_T("Failed Free!"));
}

MessageBox(_T("Successfully Send!"));

system("cls");

 Detaches the calling process from its console.


BOOL WINAPI FreeConsole(void);


是挺奇怪,感觉有点像是之前的输出动作未清除

查出来了,是这一行出问题了
while (cin >> load.container[i])

第二次执行时,这里执行失败了。

解决了,只要在关闭stdin前,执行cin的清除动作即可
cin.clear();

system("clr");就可以清空控制台窗口里面之前打印的内容