Thread: higher level directory

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    higher level directory

    Code:
    #include <stdio.h>
    
    int main(){
    
    
    int j =0;
    
    
    
    for( ; j < 3 ; j++){ 
    
    int pid= fork();
    
    if(pid ==0 ){
    
    if(j==0)execlp("pwd",  NULL);
    else if(j == 1) execlp("cd", "..", NULL);
    else execlp("pwd", NULL);
    
    }
    else{
    
    wait(0);
    }
    
    
    }
    
    return 0;
    }
    The output of pwd is the same
    how can i go to higher level directory?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    AFAIK, the shell commands that you're giving aren't related to each other. This is a much better way to do it (I assume you're on linux/unix/bsd): File Access and Directory System Calls

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    what would happen if you pass invalid arguments? Will it crash the program?

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    That execlp function looks strange. I assume you're running Unix, since you're trying to execute the pwd command. Try something like this.

    Code:
    #include <unistd.h> /* Needed for fork function */
    
    /* code code code... */
    
    for (j = 0; j < 3; x++)
    {
         int pid = fork();
         if (pid == 0)
         {
              if (j == 0) system("pwd");
              else if (j == 1) system("cd ..");
              else system("pwd");
         }
         else {
              wait(0);
         }
    }
    That should work better for you.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Most of the time, using the shell isn't a very good way of doing this especially if library calls are available for the exact purpose...

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    No Babkockdood still it doesnt work

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It would help if you would SPIT IT OUT - what OS are you on?

    When two posters take the time to mention it, it's time to quit being coy, isn't it?

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Doesn't chdir() function work?

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    no dude

  10. #10
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    What operating system are you using?!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Higher level ALU simluation
    By monki000 in forum C Programming
    Replies: 3
    Last Post: 04-20-2010, 10:48 PM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. The problem with higher level math
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-20-2004, 07:53 AM
  4. Higher level book
    By CheesyMoo in forum C++ Programming
    Replies: 9
    Last Post: 05-01-2003, 07:29 PM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM