This开发者_JAVA技巧 was prompted by some experimenting with C/C++; neither the statement
printf("\a");
nor the statement
cout << "\a";
produce an audible bell sound. How is the \a supposed to function . . .
Although the ASCII BEL control character has a well-defined meaning, its exact interpretation in your console is highly platform dependent. C defines \a
as the escape sequence for the ASCII BEL character found at code 7 and sometimes known as Ctrl+G. In the good old days, this control code allowed the bell of a remote terminal to be rung to alert the remote operator that a message needed attention.
In modern systems, the bell has largely been replaced by other mechanisms. You console window (whether an xterm or similar on *nix or a Console Windows on Windows) now has the responsibility of doing something "sensible" with that control character. It also has the freedom to ignore it completely.
On Windows, you can achieve an audible alert by calling MessageBeep()
, passing 0xffffffff
to get a simple beep noise that uses no system resources, MB_OK
or another MB_ICON*
constant to get one of the system event sounds.
Be thankful. I guess you have not been misfortunate enough to have tried displaying the listing of a binary file in a busy office.
You may have access to the aplay utility (GNU/Linux, etc.):
#include <cstdlib>
int main(int argc, char* argv[])
{
system("yes \"ZaaZa\" | aplay -c 2"); // stop this with ctrl-c
}
You can send whatever raw sound (or with some encodings, see the -f
option) to aplay. Type man aplay
for its manual.
I'd look at the configuration of your console. I don't know how Windows handles it, but on Linux, the console can be configured to ignore the system bell, or play a custom sound in its place. I'd try looking at the console's configuration as a first step before moving on to more complex solutions like aplay.
The terminal software assigned to STDOUT
decides what to do with the \a
character. You may try using another terminal software. Or dig deep into the settings of your terminal. Maybe there is a switch to enable/disable the bell sound.
精彩评论