谁能告诉我如何在 Linux 中使用 C 的 sound() 和 nosound() 函数?我可以通过包含 dos.h 头文件在 Windows 中使用它,但我在 Linux 上,所以我不能在那里使用 dos.h。
最佳答案
不幸的是,似乎没有简单的解决方案。你可以定义一个蜂鸣函数,比如
/* beep.c - for Linux and DOS/Windows */
#include <stdio.h>
#include <stdlib.h>
#ifdef __DJGPP__
#include <dos.h>
#include <pc.h>
#endif
#define ESC 27
void beep (int frequency, int duration)
{
#ifdef __DJGPP__
sound (frequency);
delay (duration);
nosound ();
#else /* Linux */
FILE *tty;
if ( NULL == (tty = fopen ("/dev/console", "w")) ) {
fprintf (stderr, "Cannot write to /dev/console!\n" );
exit (1);
}
fprintf(tty, "%c[10;%d]%c[11;%d]\a", ESC, frequency, ESC, duration);
#endif
}
int main (int argc, char *argv[])
{
int frequency, duration;
if (argc != 3) {
fprintf (stderr, "Usage: beep <frequency> <duration>\n" );
exit (1);
}
frequency = atoi (argv [1]);
duration = atoi (argv [2]);
beep (frequency, duration);
return (0);
}
/* end of beep.c */
Linux 中没有 dos.h 等价物。您需要从 linux 中可用的内容中探索
检查 here
关于c - Linux 中 C 的 sound() 和 nosound() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173025/