美文网首页
C调用python脚本实例

C调用python脚本实例

作者: rmrfany | 来源:发表于2018-12-17 15:26 被阅读0次

    仅供参考

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <assert.h>
    #include <syslog.h>
    #include <unistd.h>
    #include <string.h>
    
    
    int process_wifi_x86(const char *ssid, const char *key, u_int32_t way,u_int32_t fre) {
        printf("%s\n", "call process_wifi_x86");
        if (access("./cmx_setWifi_x86.py", X_OK) < 0) {
            return -1;
        }
    
        char cmd_buf[256];
        memset(cmd_buf,0, sizeof(cmd_buf));
        const char* cmd_str = "python cmx_setWifi_x86.py";
        sprintf(cmd_buf, " %s %s %s %d %d", cmd_str, ssid, key, way, fre);
        printf("%s\n", cmd_buf);
    
        FILE *stream;
        char result[1024*5];
        memset(result,0, sizeof(result));
    
        stream = popen(cmd_buf, "r");
        if (stream == NULL) {
            printf("%s","Failed to run command\n");
            pclose(stream);
            exit(-1);
        }
    
        /*fread( buf, sizeof(char), sizeof(buf),  stream)
        第二个参数表示每次读的个数,第三个参数表示读多少次
        */
        int ret = fread(result, sizeof(char), sizeof(result), stream);
        if (ret > 0){
            printf("%s", result);
        }else{
            printf("%s","fread err");
        }
        pclose(stream);
        return ret;
    }
    
    int main()
    {
        printf("%s\n","==========MAIN RUN==========");
        int ret = process_wifi_x86("cnm", "nmb", 6, 9);
        if(ret > 0 ){
            printf("%s\n","sucess");
        }else{
            printf("%s\n","failed");
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C调用python脚本实例

          本文链接:https://www.haomeiwen.com/subject/fqcckqtx.html