简体   繁体   English

如何更改 ac 程序中的当前目录?

[英]How can I change my current directory in a c program?

I'm new in c programming and I'm asking for a simple way to change my current directory in a programm.我是 c 编程的新手,我要求一种简单的方法来更改程序中的当前目录。 I tried to use system("new path") but it didn't work.我尝试使用system("new path")但它没有用。

The reason it is not working is that system is starting a separate process, changing its working directory, then exiting.它不工作的原因是系统正在启动一个单独的进程,更改其工作目录,然后退出。 Your program needs to change its own directory by calling chdir .你的程序需要通过调用chdir来改变它自己的目录。

If you're using a unix based OS, use chdir() .如果您使用的是基于 Unix 的操作系统,请使用chdir() For windows you'll have to use _chdir() , although an alias chdir exists, it is deprecated.对于 Windows,您必须使用_chdir() ,尽管存在别名 chdir,但它已被弃用。

Both the functions return 0 on success and -1 if an error occurred.这两个函数在成功时返回 0,如果发生错误则返回 -1。

You can only change the directory in which the program executes.您只能更改程序执行的目录。 You can't change the working directory of your shell.您无法更改 shell 的工作目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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