[英]extract values from the command output in c
我正在運行這個命令
cat /proc/devices/memory/events/pcie0_read
在我的代碼(c 應用程序)中。 這是我的代碼
#include<stdio.h>
#include<stdlib.h>
void main()
{
system(" cat /proc/devices/memory/events/pcie0_read ");
}
這個命令的 output 是
fidt=0x12,dtw=0x33,id=0x67
我只想使用相同的 c 應用程序從命令 output 中提取值。
我只想提取 0x336712 並將這個值保存在來自上述命令 output 的變量中。
例如:
char var[100] or unsigned int var;
從命令 output 提取后,我應該得到它,
var=0x336712
我怎么做???
除非您真的需要運行外部命令,否則您可能希望像讀取任何普通文件一樣直接讀取/proc/...
此外, fscanf function 非常通用,足以避免復雜的字符串解析。
#include <stdio.h>
int main() {
unsigned int fidt, dtw,id;
FILE *f = fopen("/proc/devices/memory/events/pcie0_read", "r");
fscanf(f, "fidt=%x,dtw=%x,id=%x", &fidt, &dtw, &id);
fclose(f);
printf("output: 0x%x\n", (id << 32) | (dtw << 16) | fidt);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.