简体   繁体   中英

extract values from the command output in c

I am running this command

cat /proc/devices/memory/events/pcie0_read

in my code (c application). This is my code

#include<stdio.h>
#include<stdlib.h>

void main()

{

system(" cat /proc/devices/memory/events/pcie0_read ");

}

and the output of this command is

fidt=0x12,dtw=0x33,id=0x67

I want to extract only values from the command output using same c application.

I want to extract only 0x336712 only and save this value in an variable from the above command output.

for ex:

char var[100] or unsigned int var;

After extracting from command output I should get it,

var=0x336712

How do I do that???

Unless you really need to run external commands, you may want to read the /proc/... directly just like any ordinary files. Also, fscanf function is quite versatile enough to avoid complicated string parsing.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM