简体   繁体   English

C程序中的echo变量

[英]echo variable in c program

I have a program written in C language(admin-secret) that has a function called authenticate. 我有一个用C语言编写的程序(admin-secret),具有一个称为authenticate的功能。 Inside this function there is a variable called "result". 该函数内部有一个称为“结果”的变量。 How do i echo this variable using another c program? 我如何使用另一个C程序回显此变量?
The purpose of this is to guess the password using strncmp return value. 目的是使用strncmp返回值来猜测密码。

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

int main(int argc, char *argv[])
{     
    char command[1000] = {0};
    int result;
    sprintf(command, "/home/alice/Public/admin-secret %s; echo  %d", argv[1], result);
    system(command);
    printf("Result: %s\n" , result);
    return 0;
}

You need to create a pipe, an easy way to do it is by using the POSIX function popen() . 您需要创建一个管道,一种简单的方法是使用POSIX函数popen()

#include <stdio.h>

int main(int argc, char* argv[])
{
    FILE *pipe;
    char line[256];
    pipe = popen("ls", "r");
    if (pipe != NULL)
    {
        while (fgets(line, sizeof(line), pipe) != NULL)
            fprintf(stdout, "%s", line);
        pclose(pipe);
    }
    return 0;
}

You can build the command with sprintf() 1 too and pass it as the first parameter to popen() , this is just to show you how you can capture the output of another program. 您也可以使用sprintf() 1构建命令,并将其作为第一个参数传递给popen() ,这只是向您展示如何捕获另一个程序的输出。


1 You should really use snprintf() to prevent overflowing the destination array 1 您应该真正使用snprintf()来防止目标数组溢出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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