GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Oct-2005, 04:55
salmaz8347 salmaz8347 is offline
New Member
 
Join Date: Oct 2005
Posts: 1
salmaz8347 is on a distinguished road

How to make Linux shell calls in a C++ program


Hello,

I am writing a C++ program. I need to make some system calls, like ls, cd, etc. I can do "ls" easily by doing system("ls") but when I do system("cd"), it does not change the directory but rather stays in the same one. I have a whole directory tree that I have to traverse through during the course of my entire program but I am not able to make "cd" calls. Is there some other way of doing a cd. For example, I have a home directory and within it, I have a directory called test1 and within it I have directory test2. I am initially in "home" but when I do system("cd test1") or even system("cd home/test1") from within my C++ program, it stays in home and doesn't change to test1. So can anyone please suggest something else that I can do? I know its something very simple but I am not able to find it so far.

Thanks a lot.
  #2  
Old 14-Oct-2005, 05:09
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: How to make Linux shell calls in a C++ program


Hi salmaz,

try inserting an extra /.
i.e "cd home//test1"

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 14-Oct-2005, 06:09
ove256 ove256 is offline
New Member
 
Join Date: Oct 2005
Posts: 20
ove256 is on a distinguished road

Re: How to make Linux shell calls in a C++ program


you can go up one level by calling:

//==============begin
system ("cd ..");
//==============end

you must type it "cd -space- .."
  #4  
Old 14-Oct-2005, 06:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to make Linux shell calls in a C++ program


Quote:
Originally Posted by salmaz8347
Hello,

I am writing a C++ program. I need to make some system calls, like ls, cd, etc. I can do "ls" easily by doing system("ls") but when I do system("cd"), it does not change the directory but rather stays in the same one.

Thanks a lot.

I am assuming your Linux shell is bash as it is with all distributions that I know about.

When you execute a system(...) call, here's what happens:

The operating system spawns a new shell, executes whatever stuff you have in the argument's string, and then returns to the function that invoked the system() call. Environment changes that were made (including the result of the "cd") can not be passed back upstream. Sorry.

In other words, each system() call starts in whatever directory it was in whenever program execution began.

For test purposes (this works in Linux, or with Windows if you use cygwin/gcc) you could try the following to illustrate the point:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
  system("echo -n '1. Current Directory is '; pwd");
  system("mkdir temp");
  system("echo 'Hi, Dave' > temp/xxx");
  system("cd temp;echo -n '2. Current Directory is ';pwd; ls -l; cat xxx");
  system("echo -n '3. Current Directory is '; pwd");
  return 0;
}


At the program's completion, look in directory temp, and you will find the new file and its contents will be a friendly greeting (to me). So the program did execute the "cd" thing (as it did the other system() things), it just couldn't stay there after the system() call that got it there.

Instead of stringing together a bunch of shell commands with separate system() calls you could create, then invoke, a shell script that executes the sequence, or you could make a single system call that strings together the shell commands, separated by semicolons, or you could write C function calls to do whatever it is that you want to accomplish, or ... (there are probably lots of other ways).

Here's the output of a run that I made with the above example program (the computer name has been changed to protect the innocent):

Quote:
[dave@mysecretcomputername cprogs]$ gcc z.c
[dave@mysecretcomputername cprogs]$ pwd
/home/dave/cprogs
[dave@mysecretcomputername cprogs]$ ./a.out
1. Current Directory is /home/dave/cprogs
2. Current Directory is /home/dave/cprogs/temp
total 4
-rw-rw-r-- 1 dave dave 9 Oct 14 06:16 xxx
Hi, Dave
3. Current Directory is /home/dave/cprogs
[dave@mysecretcomputername cprogs]$ pwd
/home/dave/cprogs

Regards,

Dave
Last edited by davekw7x : 14-Oct-2005 at 07:33.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
Mozilla Thunderbird dsmith Computer Software Forum - Linux 9 01-Mar-2005 11:56
Linux Kernel Upgrade Mini Howto dsmith Computer Software Forum - Linux 3 05-Apr-2004 22:10
Call a C program through Linux shell script nuwandee C Programming Language 3 29-Mar-2004 21:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:50.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.