• C

system call ( cd filename) in C

I am tried using functions system(), popen() in C, to execute the system call (cd filename, cd .. , other cd calls) in UNIX Environment.
but was unable to do so,

however
...
sytem(mkdir filename);
...

  is working fine. its just the "cd calls" that is not fine.
i would like to open the directory on running the program. eg) >>cd folder
                                                                                             should display in unix.

Kindly help please by attaching a snippet

Thanks
suda5181Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
system("mkdir filename");
system("cd filename; touch file");
chdir("filename");
system("ls -l");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zebadaCommented:
change working directory?
int chdir(const char *path);
0
zebadaCommented:
I need to learn to type faster :(
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi suda5181,

The system() function will execute any command available to your program, including "cd pathname".  In fact, the "cd pathname" that you tried to execute DID work, but it didn't do what you expected.

The system() function starts a new task to execute the command.  When you stop and think about it, it has to.  Your program is already running and you certainly don't want the "cd pathname" command to take the place of your program.  You want it to execute as if it were part of your program.

But how to make a system command work as part of your program isn't always obvious.  The cd command is actually a C program that wraps around the chdir() function.  You can call chdir() directly from your program.  It is part of the C library and can be linked with any C module.

The ch command is a different matter.  To execute a system command from within your program, the program initiates it with the system() function.  system() starts another task and in that tasks starts a command line shell.  The shell then executes the command and exits.  Since the shell is running in a separate task, it has no directed effect on the program that you're running.  This is probably not what you had in mind, and since you're trying to change to another directory it is certainly not what you wanted.

You can think of it in terms of multiple windows.  Whether on a Windows, Linux, or unix platform you can open multiple windows on the desktop.  In one of the windows you then do a "cd pathname".  This doesn't affect the other windows.  This is exactly like executing system("cd pathname") except that system opens the new task for you.

ozo and zebada are correct.  To change directories within your program use the chdir() function.

Good Luck,
Kent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.