![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Running executables in C++
Am I able to run an executable with a c++ program? Does that make any sense?
__________________
|
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Yes, you are, and yes, it does. The quick and dirty way is with the "system" function. Other ways vary, according to OS. For instance, see the execl* family of functions.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Thanks
__________________
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
all right i need some more help with this i put in this for my code but it does not work it gives me an error
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
system("c:/documents and settings/David.crawford/my documents/test.bat");
system("PAUSE");
return EXIT_SUCCESS;
}'c:/documents' is not recognized as an internal or external command, operable program or batch file. #include <cstdlib>
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
system("c:test.bat");
system("PAUSE");
return EXIT_SUCCESS;
}
__________________
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
does anyone know?
__________________
|
|
|
|
|
|
#6 |
|
Professional Programmer
|
I think you need "c:\\documents and settings\\David.crawford\\my documents\\test.bat"
|
|
|
|
|
|
#7 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
it looks like it's getting messed up by the spaces in "Documents and Settings". Try escaping those... I don't remember if that worked around it or not...
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
Like andro said you maybe needs "c:\\documents and settings\\David.crawford\\my documents\\test.bat".
It's just like when you're going in some directory, up in the addressbar the slashes isn't /, but \, but in a browser the slashes is /. If the string had been "c:\documents and settings\David.crawford\my documents\test.bat" it will not work because that a \ indicates that there's going to be a command like \n, \t, \a etc. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
You don't need to escape the spaces. You need to convince the windows command interpreter that a name can have spaces in it. The way to do that is to wrap the whole command in quotes.
For example; system("\"c:/documents and settings/David.crawford/my documents/test.bat\""); system("\"c:/documents and settings/David.crawford/my documents/test.bat\" ABC"); |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
Yup, this happens in all platforms. For example, if in my terminal (unix) I type
open -a disk utility if I put this instead: open -a "disk utility" system("open -a \"disk utility\"");The error you have encountered is because of your fault in the underlying MS-DOS prompt, and not in your program exactly. The commands you enter in the system() command are the commands that are normally accepted by the MS-DOS prompt (I imagine you are using Windows.). In OS X, the equivalent of MS-DOS prompt is 'Terminal'. Normally in my system (OS X), (I don't know if that applies for you also) in such commands in my terminal I have to use the quotes whenever an argument -such as a file path- contains spaces. That happens because when the terminal reads the command you have given, it often separates the arguments according to the spaces you have given. The quotes - in file paths and file names - force the terminal to accept every character inside the quotes as one argument and tells it not to separate each word and accept it as an argument. Be careful. C++ programming will not be the only place where you will encounter this problem. Now as far as your problem is concerned: c:/documents and settings/David.crawford/my documents/test.bat "c:/documents and settings/David.crawford/my documents/test.bat" system("\"c:/documents and settings/David.crawford/my documents/test.bat\"");You will probably learn more about system() command in C++ if you mess around with the Windows' command prompt. I hope I helped.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|