Solved

system call ( cd filename) in C

Posted on 2003-11-29
4
1,075 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

864 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

22 Experts available now in Live!

Get 1:1 Help Now