system()函数和当前工作目录 [英] system() function and current working directory

查看:298
本文介绍了system()函数和当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,解决方案应该在windows和unix下运行

操作系统。


假设我在目录X中有一个程序ex.c (所以ex.c的当前

工作目录是X)。


还假设我有这段代码片段:

....

char otherpath [PATHLEN];

char cmd [CMDLEN];

....

....

otherpath =" Y" //其中y是有效的路径字符串

....

system(cmd); //其中cmd是任何命令字符串

....


如果在cmd中指定了相对路径,它相对于当前的

工作目录(在这个例子中是X)。


有一种方法可以执行命令,这些路径是

"的相对路径。 otherpath"变量(在这个例子中是Y)?

I have a problem and the solution should works under windows and unix
OS.

Suppose I have a program ex.c in the directory X (so the current
working directory of ex.c is X).

Also suppose I have this code fragment:
....
char otherpath[PATHLEN];
char cmd[CMDLEN];
....
....
otherpath="Y" //where y is a valid path string
....
system(cmd); //where cmd is any command string
....

If in cmd is specified a relative path, it is relative to the current
working directory(in this example is X).

There is a way to execute command wich paths are relatives to the
"otherpath" variable (in this example is Y)?

推荐答案

3月21日上午9:18,gio < giorginooo.ro ... @ yahoo.itwrote:
On Mar 21, 9:18 am, "gio" <giorginooo.ro...@yahoo.itwrote:

我有问题,解决方案应该在windows下工作并且unix

OS。
I have a problem and the solution should works under windows and unix
OS.



好​​的,所以你正在寻找一个便携式的解决方案。

OK, so you are looking for a "portable" solution.


假设我在目录X中有一个程序ex.c(因此ex.c的当前

工作目录是X )。
Suppose I have a program ex.c in the directory X (so the current
working directory of ex.c is X).



[snip]

[snip]


有一种方法可以执行命令,路径是
" otherpath"变量(在这个例子中是Y)?
There is a way to execute command wich paths are relatives to the
"otherpath" variable (in this example is Y)?



不幸的是(对你而言),标准C无法识别(更不用说
提供操作工具)目录或程序执行路径。 br />
你的答案在别处,因为我们在这里帮不了你。


< OT>

很可能你将找不到一个简单,便携的解决方案,你的程序将在Unix和

Windows环境中无需修改即可运行。对于Unix,

system(PATH = Y:

Unfortunately (for you), standard C does not recognize (let alone
provide tools to manipulate) directories or program execution paths.
Your answer lies elsewhere, as we can''t help you here.

<OT>
It is likely that you will not find a simple, portable solution to
your program that will work without modification in both the Unix and
Windows environments. For Unix,
system("PATH=Y:


PATH cmd);

可能有效。但是,shell中的这个技巧不太可能适用于Windows。

< / OT>


HTH

-

Lew

PATH cmd");
may work. However, it is unlikely that this trick in the shell will
work for Windows.
</OT>

HTH
--
Lew


这可能是一个解决方案:


构建一个新的cmd,例如:

char * newcmd = malloc(3 + strlen(otherpath)+ 2 + strlen(cmd)+1);

sprintf(newcmd," cd%s;
This could be a solution:

Build a new cmd, e.g.:
char *newcmd = malloc(3+strlen(otherpath)+2+strlen(cmd)+1);
sprintf(newcmd, "cd %s;


这篇关于system()函数和当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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