In C language, you can use the system function to call system commands and obtain the output. You can also save the output of program execution to a file for use through output redirection, but it is not very convenient to use. I will introduce how to use python and go to save the output of other programs as variables.
The following example uses the ls name. You need to install MinGW and add "C: \ MinGW \ msys \ 1.0 \ bin" to the environment variable.
1. Call other programs in python and obtain the output
Sample Code:
Copy codeThe Code is as follows:
Import OS
Var = OS. popen ('LS-l'). read ()
Print var
Running Effect (take my machine as an example ):
2. Use the go language to call other programs and obtain the output
Go code:
Copy codeThe Code is as follows:
Package main
Import (
"Exec" // "OS/exec" in go1
"Fmt"
)
Func main (){
Cmd: = exec. Command ("ls", "-l ")
Buf, err: = cmd. Output ()
Fmt. Printf ("% s \ n % s", buf, err)
}
The running effect is as follows: