C - 从字符串中获取 CLEAN 数字变量

标签 c bash

我一直在为一个小型 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/

相关文章:

c - 为什么 static 关键字在这里防止段错误?

c - Linux 中的 Select 循环——如何让它变得更好?

c++ - C/C++ 中 p[i] 、 i[p] 和 *(p+i) 、 *(i+p) 的区别

c - 为什么要用大括号将 C 代码块括起来?

c - 请解释以下 C 程序的输出?

linux - 为什么 "kill"命令在 bash 和 zsh 中的工作方式不同

bash - 如何终止所有子shell进程?

linux - 使用curl解析时区

json - Bash 和 jq - 查找变量中包含的键并更改其值

macos - 在 Mac 中,如何在脚本 (sh/bash/applescript) 中确定当前是否通过 Apple Remote Desktop 运行?