简体   繁体   中英

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. I tried to use system("new path") but it didn't work.

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 .

If you're using a unix based OS, use chdir() . For windows you'll have to use _chdir() , although an alias chdir exists, it is deprecated.

Both the functions return 0 on success and -1 if an error occurred.

You can only change the directory in which the program executes. You can't change the working directory of your shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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