Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

system call ( cd filename) in C

Posted on 2003-11-29
4
1,077 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:Kent Olsen
Kent Olsen 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to find a data that is pure string   or not in vb.net ? 9 114
reading tzdatabase for timezone definitions 5 142
How to organize data in excel ? 2 115
Line meaning 9 87
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to create, access, and change arrays in the C programming language.

791 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