system() usage?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: system() usage?

  1. #1
    Join Date
    Jul 2009
    Posts
    8

    Question system() usage?

    I'm a tad new at this language so bare with me:

    #include <iostream>

    int main()
    {
    char driveLetter[1];
    puts("What is the drive letter of the Device?: ");
    gets(driveLetter);

    puts("Starting to Copy...");
    system("copy C:\\Documents and Settings\\User\\Desktop\\dir1\\*.* " << driveLetter << ":\\backup");

    return 0;
    }

    this naturally returns an error : invalid operands of types `const char[53]' and `char[1]' to binary `operator<<'

    What's going wrong here :(

    Thanks for any advice/help!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    you need to #include <cstdlib> and probably <cstdio>.
    Danny Kalev

  3. #3
    Join Date
    Dec 2007
    Posts
    401
    > char driveLetter[1];

    a c-style string needs an extra null character at the end as a terminator.
    char driveLetter[2];

    the use of the gets() function, because of its lack of bounds checking, and the inability for the calling program to reliably determine the length of the next incoming line, constitutes a vulnerability in code. it enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack.
    in C code, the fgets() function is the better option in all cases. in C++ code, it is simpler to just use std::cin. in this case, just read a single character from standard input.

    the system() function hands over the argument (a c-style string), to the standard command interpreter as a command to be executed. you have to make one string for the entire command, the easy way to do this is to use the overloaded + operator in the std::string class.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    int main()
    {
       char drive_letter ;
       std::cout << "What is the drive letter of the Device?: " ;
       std::cin >> drive_letter ;
    
       std::cout << "Starting to Copy..." << std::flush ;
       const std::string SRCE_PATH = "C:\\Documents and Settings\\User\\Desktop\\dir1\\*.* " ;
       const std::string CMD_STRING = "copy " + SRCE_PATH + ' ' + drive_letter + ":\\backup" ;
    
       return std::system( CMD_STRING.c_str() ) ;
    }

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. File system API for project
    By aanchs in forum Java
    Replies: 0
    Last Post: 03-10-2006, 09:08 AM
  3. Replies: 0
    Last Post: 07-08-2005, 07:01 AM
  4. API For shutting, starting win NT,9x OSes
    By Saiful in forum VB Classic
    Replies: 6
    Last Post: 10-15-2000, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center