Computing.Net > Forums > Programming > Can I do system(variable); ??

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Can I do system(variable); ??

Reply to Message Icon

Original Message
Name: change2linux
Date: December 13, 2005 at 12:22:20 Pacific
Subject: Can I do system(variable); ??
OS: Fedora Core 4 (Linux)
CPU/Ram: 500mhz, 224mb RAM
Comment:

Hey,
Is it possible to enter a terminal command, through a
C++ program, and then have it execute it?
What I mean is the user enters a command, within a C++
program, and the C++ program then executes it. Is there something along the
lines of a statement like:

system(variable_name);

It's difficult to explain, if anyone needs more info just say
so - no problem. Thanks!


Report Offensive Message For Removal


Response Number 1
Name: finitestateautomaton
Date: December 13, 2005 at 12:28:55 Pacific
Reply: (edit)

Hi! change2linux, system(const char*) executes any string given to it as parameter by passing it to the command interpreter. Eg: system("dir") executes the dos directory command. I hope this helps :)


Report Offensive Follow Up For Removal

Response Number 2
Name: change2linux
Date: December 13, 2005 at 16:29:24 Pacific
Reply: (edit)

My current source is:

#include <iostream.h>
#include <stdlib.h>

int main()
{
char cmdthing;
std::cout << "Command: ";
std::cin >> cmdthing;
std::cout << endl;
system (char * cmdthing);
return 0;
}

and I get the error "parse error before `*' token" is there
something I'm doing wrong?


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: December 14, 2005 at 07:27:20 Pacific
Reply: (edit)

I'm not a C++ guy, but here's a couple of things wrong on the "C" side: cmdstring is defined as a single char so you are running over the buffer.

Consider this stub which doesn't ask for input, but shows you how to use system correctly:

#include <stdio.h>

int main()
{
/* make sure the input buffer is long enough*/
char cmdthing[50];

strcpy(cmdthing, "ls myfile");

system(cmdthing);

}


Report Offensive Follow Up For Removal

Response Number 4
Name: finitestateautomaton
Date: December 14, 2005 at 10:34:59 Pacific
Reply: (edit)

Try this code

#include <iostream.h>
#include <stdlib.h>

int main()
{
char cmdthing[256];

cout << "Command: ";
cin >> cmdthing; //if you want to read a string with spaces then replace this line by cin.getline(cmdthing, 256, '\n');
cout << endl;

system(cmdthing);

return 0;
}

You intend to use "cmdthing" for storing a string so you must declare it as an array ie "char cmdthing[256]"; "char cmdthing;" only declares "cmdthing" as character(not an array). When you are calling the function just use the variable name as the parameter ie simply "cmdthing" eg: "system(cmdthing);". I hope this helps :).


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software