-
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!
-
you need to #include <cstdlib> and probably <cstdio>.
Danny Kalev
-
> 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
-
Replies: 5
Last Post: 05-27-2008, 11:17 AM
-
Replies: 0
Last Post: 03-10-2006, 09:08 AM
-
Replies: 0
Last Post: 07-08-2005, 07:01 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks