Thread: system() function using

  1. #1
    Registered User
    Join Date
    Jul 2007

    Question system() function using

    hello ..
    i m new at c++ programming. i have a problem about system() using.
    you know system( ) function lets us to use dos commands.
    and i have to use it like


    my problem starts here. i want to use commands with variable.

    for ex :
    char folder[10] ;
    cout<<"which folder"<<endl; // for ex i typed " photos " ;
    system("dir folder[10]") // here is my problem.
    i can not use system function with variable. i want to use that function to effect the folder i choose. but system function does what is written between quotation marks. So it's looking for the folder named "folder[10]" . i want it to effect the photos folder.
    so here are my questions ;

    how can i use system function with variable ?
    or is there anyway to use dos commands for the variables (folders) i choose ?
    or is there any other way to do this ?

    i m newbie.. i will be glad if someone can help, any help would be appreciated.
    Thanx in advance.
    Last edited by Rommeo; 07-26-2007 at 10:51 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    1. stop using C-strings, use std::string
    2. do not use >> to read input - it stops on spaces, so you cannot read folder name with spaces inside - use getline
    3. use stringstream to generate result
    std::stringstream ss;
    ss<< "dir " << folder;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Well, you can use sprintf/snprintf/strings/stringstreams to accomplish this.

    Using strings:
    string command = "dir ";
    string folder;
    cout << "which folder" << endl;
    cin >> folder;  // Type in "photos"
    command += folder;  // Now command contains "dir photos"
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM