Move text cursor to particular screen coordinate?

后端 未结 7 1844
余生分开走
余生分开走 2020-12-31 19:01

How can I set the cursor at the desired location on the console in C or C++?

I remember a function called gotoxy(x,y), but I think its deprecated. Is th

7条回答
  •  长发绾君心
    2020-12-31 19:39

    I figured out this to set the cursor.

    #include 
    
    void setPos(std::ostream& _os, const std::streamsize& _x, const std::streamsize& _y)
    {
        char tmp = _os.fill();
    
        if(_y>0) {
                _os.fill('\n');
                _os.width(_y);
                _os << '\n';
        }
        if(_x>0) {
                _os.fill(' ');
                _os.width(_x);
                _os << ' ';
        }
        _os.flush();
        _os.fill(tmp);
    }
    
    int main(int argc, char **argv)
    {
        setPos(std::cout, 5, 5);
        std::cout << "foo" << std::endl;
        return 0;
    }
    

    To do more you'll need assumptions on the resolution or a lib like ncurses.

提交回复
热议问题