Solved

system call ( cd filename) in C

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

810 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