Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2006, 6:09 PM   #1
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Running executables in C++

Am I able to run an executable with a c++ program? Does that make any sense?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Jun 23rd, 2006, 6:23 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 23rd, 2006, 10:50 PM   #3
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Thanks
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Jun 23rd, 2006, 11:13 PM   #4
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
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;
}
the error is this:
'c:/documents' is not recognized as an internal or external command,
operable program or batch file.
but if i put in something like this...
#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;
}
it works just fine. (I do have the "text.bat" in both directories) why does the first one not work?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Jun 23rd, 2006, 11:45 PM   #5
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
does anyone know?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Jun 24th, 2006, 12:00 AM   #6
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
I think you need "c:\\documents and settings\\David.crawford\\my documents\\test.bat"
andro is offline   Reply With Quote
Old Jun 24th, 2006, 12:26 AM   #7
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
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...
Jimbo is offline   Reply With Quote
Old Jun 24th, 2006, 12:31 AM   #8
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
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.
v0id is offline   Reply With Quote
Old Jun 24th, 2006, 2:28 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
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\"");
or (if you want to pass a parameter ABC to the batch file)
    system("\"c:/documents and settings/David.crawford/my documents/test.bat\" ABC");
grumpy is offline   Reply With Quote
Old Jun 24th, 2006, 3:12 AM   #10
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Yup, this happens in all platforms. For example, if in my terminal (unix) I type
open -a disk utility
it will not find the application, simply because it searches for the application named "disk".

if I put this instead:
open -a "disk utility"
the application will open just fine. But if I want to give this command trough the 'system()' command of C++, I will simulate the quotes with the '\"'. so the whole system() command in C++ will be like this:
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
If you write that in your MSDOS command prompt, it will probably not work, because of the spaces in the file path. If you give
"c:/documents and settings/David.crawford/my documents/test.bat"
it will accept it and proceed with your command. Now, you must use the same commands inside the system() function in your C++ compiler. So, to simulate the quotes, you must give this:
 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)
Soulstorm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC