How to assign the return value of a shell command to an application's variable.
Bo Master recently made a router project, need to get the router now network status and parameters, using the UCI get GET,
However, with system ("UCI get"), the obtained parameters are printed out directly in the terminal, which is not available in the application.
The returned parameter. Here is my solution.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Char buf[512];
char* Uciget (char option[])
{
FILE *stream;
memset (buf, ' n ', sizeof (BUF));
stream = popen (option, "R");
Fread (buf, sizeof (char), sizeof (BUF), stream);
Buf[strlen (BUF) -1]= ' + ';
Pclose (stream);
return buf;
}
int main (void)
{
printf ("%s\n", Uciget ("UCI get Network.wan.proto"));
printf ("%s\n", Uciget ("UCI get [email Protected][0].ssid"));
return 1;
}
Sun Qiqung accompany you to learn--how to assign the return value of a shell command to the application's variables