使用系统命令在Windows下从C程序中改变目录 [英] changing the directory from inside a c program under windows using system command

查看:232
本文介绍了使用系统命令在Windows下从C程序中改变目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我必须从C程序中运行命令提示符命令。下面是我做的。

I have a problem where I have to run a command prompt command from inside a C program. Here is what I did

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


int main(){



system("cd \Users");
system("Dir");

}

问题是我想cd到用户,然后再执行命令dir其中,按照目前的方案产出,它找不到指定的路径和目录在父进程目录,即其中的程序源文件所在执行。

The problem is I want to cd into Users first and then execute the command Dir where as currently the program outputs that it cant find the path specified and the Dir is executed in the parent process directory i-e where the program source file is located.

实际的任务,我想要做的就是想从C程序中运行,从一个特定的目录中的Java文件。了Java文件位于C:\\用户\\阿卜杜拉\\亲。我的C语言程序的父目录为C:\\用户\\ Cprog。请我怎么可能做到这一点建议

The actual task I want to do is just want to run a java file from a particular directory from inside a C program. that java file is in C:\Users\Abdullah\pro . My C program's parent directory is C:\Users\Cprog. Please advise on how may I do this

推荐答案

您的程序有一些不正确的假设。首先,CD和DIR不是程序,但命令内置于壳,cmd.exe的。第二,我怀疑你并不需要在所有改变当前目录。

Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.

无论哪种方式,因为这是Windows系统,我会的看看如何下手的CreateProcess()

Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().

有关改变当前目录,检查出的的的CreateProcess()调用的参数lpCurrentDirectory

For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.

这篇关于使用系统命令在Windows下从C程序中改变目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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