Why running an executable through cmd does not invoke system() call?

一个人想着一个人 提交于 2019-12-11 15:27:45

问题


I have a small piece of C code as below. I have tried to run it using 2 methods.

1) With Clion using Cygwin64 environment

2) With command prompt (in this case, I have to move a cygwin1.dll to the same folder with the executable).

My code need to call the system() function to run some cmd command.

If i tried the 1st method, the code works flawlessly. However, when using the 2nd method, the system() call seems to be doing exactly nothing.

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

int main() {

    FILE *fp=fopen("run.bat", "w+");
    fprintf(fp,"dir > result.txt\n");
    fclose(fp);
    printf("Before calling System\n");
    system("cmd.exe /c run.bat");
    if(access("result.txt",F_OK)==0){
        printf("Run completed!\n");
    }
    printf("After calling System\n");

}

What I get for doing with 1 is the line "Run completed!" got printed out normally.

However, with 2, no "result.txt" was created, and thus the "Run completed!" line never appears.

Now I need my executable to be executable in cmd to be of any use. So can anyone help?


回答1:


I was able to solve the problem, but not in a very convenient way.

I have to download Microsoft Visual Studio, and run Clion through with VS environment, and fix certain compatibility issue.

I suppose while most of the function work fine, system() is an exception. This command is not very widely used anyway, but be aware while using it on Windows with some Linux compiler.



来源:https://stackoverflow.com/questions/55576567/why-running-an-executable-through-cmd-does-not-invoke-system-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!