Why running an executable through cmd does not inv

2019-08-30 01:46发布

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条回答
放荡不羁爱自由
2楼-- · 2019-08-30 02:23

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.

查看更多
登录 后发表回答