这是一个简单的 bash 代码,用于在终端中显示仪表:
#!/bin/bash
{
for ((i = 0 ; i <= 100 ; i+=5)); do
sleep 0.1
echo $i
done
} | whiptail --gauge "Please wait while we are sleeping..." 6 50 0
# you can replace 'whiptail' with 'dialog', it will work.
我想在 C 中重现同样的事情。因此我这样做:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
// set DIALOG to "dialog" or "whiptail"
#define DIALOG "whiptail"
int main()
{
FILE* pipe;
if( (pipe = popen(DIALOG " --gauge 'Loading...' 6 50 0","w") )!=NULL)
{
int i;
for (i=1; i<=100; i++)
{
usleep(0.1);
fprintf(pipe, "%d\n",i);
fflush(pipe);
}
pclose(pipe);
}
return 0;
}
但它只能与“dialog”一起使用,我无法让它与“whiptail”一起使用:(
有什么帮助吗?
解决方案 如Brad S.解释一下,如果太快了...更改为 usleep(100000) 就可以了
最佳答案
如果我 sleep 时间更合理,它对我有用......尝试:usleep(100000);
关于c - C 型鞭尾压力表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335514/