Sign in to follow this  
gila kid

Variables & MS-DOS Commands

Recommended Posts

Hello. I recently had an idea to write a short and quick program that will ask for input of a directory. The program would then create a directory using teh system function in c++. Here is my code: #include <iostream> #include <string> using namespace std; int main() { string fileName; cin>>fileName; system("md "+fileName); return 0; } basically i was just wondering how you can put variables in with teh system commands.. Thank YOu' -Gila

Share this post


Link to post
Share on other sites
std::string is an std::string - most functions, especially these WinAPI functions that have been around since before std::string even existed - take C-style strings. Fortunately it's easy to convert your std::strings to C-style...


string test;
cout << "Enter a system command: ";
cin >> test;
system( test.c_str() );



That's to say, stringname.c_str() returns your std::string as a const char*. Hope it helps.

EDIT: For use in the code you posted, system("md" + fileName.c_str());
EDIT: Beat to the punch!

Share this post


Link to post
Share on other sites
You pass a character array to system(). That means that you can just create a string and pass it's character pointer, like so:


std::string str;
std::string filename;

std::cout << "Enter filename: ";
std::cin >> filename;

str = "md ";
str = str + filename;

system(str.c_str());


There may be some syntax errors in there, or the + operator might not work like that, but you get the idea.


EDIT: too slow....

Share this post


Link to post
Share on other sites
That fails to compile because you need to provide a char* to system() and the addition of char* and string is not defined, however the addition of a string and a char* is defined because string has a string operator+( const std::string&, const std::string& ) (and also a string operator+( const std::string&, const char* ).
So you can use

system( (string( "md " ) + fileName).c_str() );




to do what you want.

edit: K.O. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Frequency
EDIT: For use in the code you posted, system("md" + fileName.c_str());


That won't work for the reason that a string can't be added to a const char* like "md", only vice-versa.

Share this post


Link to post
Share on other sites
You need to look at argc, and argv.
int main(int argc, char **argv)
{
return 0;
}

argc is going to have the number of arguments.
argv is going to have a array of each command, seperated by spaces.

Example;
myprogram.exe mynewdirectory

argc = 2
argv[0] = "myprogram.exe";
argv[1] = "mynewdirectory";

You can use this to make a directory with a program :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this