Thread: Basic system calls help.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Basic system calls help.

    Hi Everyone, its been a little while since i posted, been lurking though!

    I'm writing a short program to try and sift through a mountain of data from some simulation results.

    Basically, the user inputs some values corresponding to some variables in the schematic and it should change to the appropriate directory where the data is and plot a file.

    The problem i'm having is that i get to where the user inputs data and i change to the directory but it doesn't stay in the directory. Is it possible to get it to do this?

    Some brief code may help explain:

    Code:
    if(width < 10)
    {
    
    printf("cd 2amp/eps/3m6to10m\n");
    system("cd 2amp/eps/3m6to10m");
    
    
    }
    else
    	if(width < 20)
    	{
    	printf("cd 2amp/eps/10mto20m\n");
    	system("cd 2amp/eps/10mto20m");
    	}
    and so on.. if i do:

    Code:
    system("cd 2amp/eps/10mto20m/; pwd")
    I get the right directory but if i do:

    Code:
    system("cd 2amp/eps/10mto20m/")
    system("pwd")
    It prints the directory the program is in so i know its going in to the directory but coming straight back out. Is there a way to keep it into the directory once its in?

    Or should i save the directory address as a variable to do later?

    Thanks in advance for your time,
    Simon.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you invoke the system() function, a new shell is created. Any changes to that shell (like the current directory), apply to that shell only. When the system function returns, that shell has gone away.

    You're probably better off saving the path information in a variable.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Thanks, i tried saving the path info in variables but i can't seem to get it to work. I'm using tcsh and the c program seems to be implementing all my system calls in bourne shell. How do i change it so that it will use tcsh?

  4. #4
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    it doesn't seem to keeo the shell variables. Even if i stick to bourne shell. ie:

    Code:
    system("W_DIR=2amp/eps/3m6to10m; export W_DIR");
    The variable is forgotten directly after the above command!

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Have you tried the chdir() function?
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Thanks a lot for your help but i've abandoned it! Really in the spirit of learning!

    Did it with a bash script instead.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quiet System Calls in C++
    By blackey in forum C++ Programming
    Replies: 17
    Last Post: 05-18-2007, 06:09 AM
  2. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  3. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  4. help with system calls
    By go4mohit in forum C Programming
    Replies: 4
    Last Post: 06-06-2005, 09:42 AM
  5. System calls
    By Mak in forum C Programming
    Replies: 1
    Last Post: 02-06-2003, 09:49 AM