为什么通过cmd运行可执行文件不会调用system()调用? [英] Why running an executable through cmd does not invoke system() call?

查看:121
本文介绍了为什么通过cmd运行可执行文件不会调用system()调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小段C代码,如下所示.我尝试使用2种方法来运行它.

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

1)在使用Cygwin64环境的Clion中

1) With Clion using Cygwin64 environment

2)在命令提示符下(在这种情况下,我必须将cygwin1.dll移动到与可执行文件相同的文件夹中).

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

我的代码需要调用system()函数来运行一些cmd命令.

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

如果我尝试了第一种方法,则代码可以完美地工作.但是,当使用第二种方法时,system()调用似乎什么也没做.

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");

}

我用1做的是运行完成!"这一行.已正常打印.

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

但是,对于2,没有创建"result.txt",因此运行完成!"第从不出现.

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

现在,我需要我的可执行文件才能在cmd中执行,以便有任何用途.有人可以帮忙吗?

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.

我必须下载Microsoft Visual Studio,并在VS环境中运行Clion,并修复某些兼容性问题.

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

我想虽然大多数功能都能正常工作,但system()是一个例外.无论如何,该命令并不是很广泛地使用,但是请注意,在Windows和某些Linux编译器上使用该命令时.

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.

这篇关于为什么通过cmd运行可执行文件不会调用system()调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆