我一直在为一个小型 LCD 项目摆弄一些 C 编程。
我有一个字符串,在 BASH(脚本)中执行,返回 0 - 100 之间的值(音量级别) 字符串是这样的:
!/bin/bash
amixer get 'DIYINHK Clock Selector',0 | awk '/[/ {getline; print $5}' | tr -d '[]%'
我需要返回这个值,以便我可以在 if/else 语句中使用它,例如 我认为该值是以 ascii 方式返回的还是其他方式?
printf 输出是正确的,例如。当amixer
为98时,屏幕上的printf为98
但在液晶屏上,98后面有一种_......就像这样:98_
它是某种“返回”值吗?
我需要的是一个干净的数字,我可以将其用作例如。 :
if (get_volume <= 30)
statement;
else if (get_volume <=50 )
statement;
else if .... .......
这是一个片段:
FILE *fpipe;
char *command = (char *)"/home/pi/lcd/get_volume.sh"; // get_volume script
char line[256];
if (! (fpipe = (FILE*)popen(command,"r")) ) { perror("PROBLEMS"); exit(1); } while ( fgets( line, sizeof line, fpipe))
lcdPosition(lcd, 0,0);
lcdPrintf(lcd, line); // Print output on LCD
printf("\nline = %s \n", line); // Print output on screen
最佳答案
atoi
函数将表示数字的字符串转换为 int。因此,要获得“干净的数字”,您需要类似以下内容:
int volume = atoi(line);
关于C - 从字符串中获取 CLEAN 数字变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21514668/