简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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