C同时输入和输出到控制台窗口

我正在写一个服务器(主要用于 Windows,但如果我可以保持多平台,这将很酷)我只是使用一个普通的控制台窗口.但是,我希望服务器能够执行诸如text_to_say_here或kick playername等命令.我怎样才能有异步输入/输出?我已经尝试了普通的printf()和gets_s的一些东西但是这导致了一些非常奇怪的东西.

我的意思是像这样的1

谢谢.

解决方法

利用C 11功能的快速代码(即跨平台)
#include <atomic>
#include <thread>
#include <iostream>

void ReadCin(std::atomic<bool>& run)
{
    std::string buffer;

    while (run.load())
    {
        std::cin >> buffer;
        if (buffer == "Quit")
        {
            run.store(false);
        }
    }
}

int main()
{
    std::atomic<bool> run(true);
    std::thread cinThread(ReadCin,std::ref(run));

    while (run.load())
    {
        // main loop
    }

    run.store(false);
    cinThread.join();

    return 0;
}

相关文章

首先GDB是类unix系统下一个优秀的调试工具, 当然作为debug代...
1. C语言定义1个数组的时候, 必须同时指定它的长度.例如:int...
C++的auto关键字在C+⬑新标准出来之前基本...
const关键字是用于定义一个不该被改变的对象,它的作用是告诉...
文章浏览阅读315次。之前用C语言编过链表,这几天突然想用C+...
文章浏览阅读219次。碰到问题就要记录下来,防止遗忘吧。文章...