writing commands to the terminal

This is a discussion on writing commands to the terminal within the C Programming forums, part of the General Programming Boards category; Ok, so heres what I would like to do. I want to be able to go to a folder (cd ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    writing commands to the terminal

    Ok, so heres what I would like to do. I want to be able to go to a folder (cd *examplefolder*) and then remove a file in that folder.. so for example.

    : cd Desktop
    : rm example.txt

    if I did that in terminal it would work but using the system() function isn't working. However I am using it in two separate functions like
    system(cd Desktop);
    system(rm example.txt);

    Now it isn't that simple I am puting char arrays in for the commands, but same basic idea. Do I need to make it all in one command?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I'm kind of tired and about to go to sleep, but I think system() spawns a new instance of the shell (ie. it runs cmd.exe), and executes the given command.

    Now a concept that you need to know is that every program has a current directory. Sometimes it's called something different, like the "current working directory", or any of a number of other titles, but the concept is that the program has some directory that is considered the current one that it can work with.

    With that said, the important thing to note is that every program has it's own current directory. That means running "cd" in a shell doesn't work for your program's current working directory.

    You might counter with asking why the shell's cwd doesn't change with the system() call with the parameter "cd ...". The answer goes back to what I said originally. Each system() call runs the given command on a new and separate shell. This means that the command to cd to your desktop folder will be executed by one instance of a shell, but the rm command will be executed by a different instance of a shell.

    The solution is generally to avoid using system() and do everything yourself in your program. Combining them into one command won't work unless you find a way to type them as one command.... possibly using some bizarre piping or something.

    Edit: Apologies for any errors. I have a feeling I might have made a few.
    Last edited by MacGyver; 11-22-2007 at 10:13 PM.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    So basically what you are saying is that you can't really delete a file with a program unless it is in the same directory as your program? Also, I am using unix in this case

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Code:
    remove("Desktop\\example.txt");
    Or, if your path separator is slash instead of backslash:

    Code:
    remove("Desktop/example.txt");
    If you really, really want to use system(), which I do NOT recommend:

    Code:
    system("cd Desktop && rm example.txt");

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    @brewbuck that was good tip. Never knew that, thank for that

    Code:
    system("cd Desktop && rm example.txt");
    Even though it is not recommand

    ssharish

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Oooooh the old &&-in-the-system()-string trick. That's the 2nd time this week I fell for it.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    You're the bomb dot com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Clearing Terminal
    By mrginger in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 12:58 PM
  2. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  3. Writing to/reading from terminal window
    By ac251404 in forum Windows Programming
    Replies: 0
    Last Post: 05-19-2006, 02:06 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21