开发者

Calling executable file inside C program

开发者 https://www.devze.com 2023-02-18 01:11 出处:网络
I have a C program which will take one argument as input and, if the argument is matching with the string inside the executable, it will return 1 otherwise 0. The executable file name is prg1. I have

I have a C program which will take one argument as input and, if the argument is matching with the string inside the executable, it will return 1 otherwise 0. The executable file name is prg1. I have some input strings in a file named inputs.txt. I want to get those strings from the input file and call the prg1 inside a C program with each string.

I have tried the following code but it's not working.There is no segmentation fault but when i am calling prg1 it executes, Because the printf() statement inside prg1 is working and i can see the output.it changes variable found to 0I cant change the prg1. Because my friend has given the executable file of that program to me, not the source code. Header files are stdio.h and string.h

int main()
{
    FILE *fk;
    char text[80],inp[16],test[50]={"./prg1 "};
    int found=100;
    fk=开发者_高级运维fopen("inputs.txt","r");
    while((fscanf(fk,"%s",inp))!=EOF)
    {
        strcat(test,inp);
        found=system(test);
        if(found==1)
        {
            printf("\nAnswer is   : %s",inp);
            break;
        }
            strcpy(test,"./prg1 ");

    }
    fclose(fk);
    return 0;
}

What is wrong with my code?


I am not sure of what you want to achieve but here are some comments:

1 - You should test the return value of fopen:

if (!fk) { ... }

2 - You're not cleaning the test buffer between each test, so you are effectively calling:

system("prg1 first_word");
system("prg1 first_wordsecond_word");
...

You should have something like:

strcpy(test, "prg1 ");

after entering the loop and before strcat.

3 - Do you have spaces in your input strings? You should fix your code to read until a newline in this case.

4 - You might want to use EXIT_SUCCESS and EXIT_FAILURE instead of 0 and 1.


prog1 returns 1 when finding match, but 1 stands for error (at least in linux systems). Try returning EXIT_SUCCESS and EXIT_FAILURE (defined in stdlib.h). Then, when the system() call returns 0, the match is found, when anything else, match is not found.


The fundamental flaw in your code is that you need to reset the contents of the 'test' array to be "prg1 " at the beginning of each iteration of the main loop, before you call strcat to add the next argument to the command line. Otherwise the command to be run will just continue to get longer with each iteration, as each input read is added to the existing command. I don't think that is what you intend.

For example, given two lines of input, "foo" and "bar", the first iteration of the loop will cause the command "prg1 foo" to be executed, while the second will cause "prg1 foobar" to be executed. An easy way to have checked that would be to insert a printf (or similar) before the call to system() to display what command is going to be executed.

You also should check the return code of fopen and check array bounds when assigning to an array using fscanf.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号