Solved

system call ( cd filename) in C

Posted on 2003-11-29
4
1,073 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:suda5181
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 9844568
system("mkdir filename");
system("cd filename; touch file");
chdir("filename");
system("ls -l");
0
 
LVL 6

Expert Comment

by:zebada
ID: 9844573
change working directory?
int chdir(const char *path);
0
 
LVL 6

Expert Comment

by:zebada
ID: 9844575
I need to learn to type faster :(
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 100 total points
ID: 9845338
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now