简体   繁体   中英

How to disable echo in windows console?

How do I disable echo in a windows console C application?

I don't really want to capture characters with _getch (I still want Ctrl-C ) to work. Besides _getch only seems to disable echo for cmd but not in cygwin .

There must be a way to redirect the pipe or modify the console settings.

Maybe SetConsoleMode (stolen from codeguru ) :

#include <iostream>
#include <string>
#include <windows.h>


int main()
{
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode = 0;
    GetConsoleMode(hStdin, &mode);
    SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

    std::string s;
    std::getline(std::cin, s);

    std::cout << s << "\n";
    return 0;
}//main

Yes, the easiest way to do it my opinion would be to use freopen to basically change the file opened behind the stdout file descriptor.

You could redirect the console output to a file using

freopen("C:\some_file.txt", "w", stdout);

If you don't want to store the output at once, you should be able to write a /dev/null like (/dev/null is in Unix), but in windows (that I don't have you can try "nul" or "\\Device\\Null"

So something like the following should work :

freopen("\Device\Null", "w", stdout);

Sorry I can't actually try that out since I don't have windows, but that's the main idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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