Calling external files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2004
Posts: 468
Reputation: TKS will become famous soon enough TKS will become famous soon enough 
Solved Threads: 18
TKS's Avatar
TKS TKS is offline Offline
Posting Pro in Training

Calling external files

 
0
  #1
Apr 19th, 2005
Hey all,


Just a quick C++ question from me...I'm sure this is info that is available on the web somewhere but a couple of searches weren't very fruitful.

Would it be possible for someone to provide an example of the following?
  • Calling/opening and executing a batch file from within the program.
What I want to do is call a batch file during a simple C++ program I'm writing for a class...pretty easy I'm sure, except the gay book we are using doesn't call files externally so I have no idea how to do it. A link to a page for help would suffice as well instead of an example. Thanks ahead of time.
My Home Away from Home: Yet Another Linux Blog
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Calling external files

 
0
  #2
Apr 19th, 2005
The only standard method is the system function:
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. int main ( int argc, char *argv[] )
  5. {
  6. if ( argc < 2 ) {
  7. std::cerr<<"Too few arguments"<<std::endl;
  8. return EXIT_FAILURE;
  9. }
  10.  
  11. // Be ultra-anal
  12. if ( std::system ( 0 ) == 0 ) {
  13. std::cerr<<"No command processor available"<<std::endl;
  14. return EXIT_FAILURE;
  15. }
  16.  
  17. return std::system ( argv[1] );
  18. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2004
Posts: 468
Reputation: TKS will become famous soon enough TKS will become famous soon enough 
Solved Threads: 18
TKS's Avatar
TKS TKS is offline Offline
Posting Pro in Training

Re: Calling external files

 
0
  #3
May 3rd, 2005
Ok...so forgive my silliness here...I've found something from the tutorials here at daniweb

  1. #include <iostream>
  2. #include <cstdlib> // You need this to call the system() function
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char prog[100];
  8.  
  9. cout << "Enter the program you want to call:\t"; // Prompt for a program and run it.
  10. cin.getline(prog);
  11.  
  12. system(prog);
  13.  
  14. return 0; }

This will work as well right? If so...I'll need some advice. What I'm trying to do is call a batch script from within my program. Thus far, I've got it prompting the user to input a string of text. In the case of my program it will be a printer share...so the user will enter:

printer/share

Like that and the program stores the share name. After this, I'll have it prompt the user for the workstation NETBIOS name in string form and store that value.

The question is...How can I get these values to pass as parameters for the code(s) above to call that external batch file? I've hunted, and I still haven't seen anything that can point me in the right direction. Links or help is appreciated!
My Home Away from Home: Yet Another Linux Blog
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Calling external files

 
0
  #4
May 3rd, 2005
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string s = "mybatchfile ";
  10. string input;
  11.  
  12. cout<<"Parameters: ";
  13. getline ( cin, input );
  14.  
  15. system ( ( s + input ).c_str() );
  16. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2004
Posts: 468
Reputation: TKS will become famous soon enough TKS will become famous soon enough 
Solved Threads: 18
TKS's Avatar
TKS TKS is offline Offline
Posting Pro in Training

Re: Calling external files

 
0
  #5
May 3rd, 2005
Cool...that's looking like what I needed...I've got everything compiled and ready...but I don't think my second parameter is being passed to the batch script....if someone could take a glance below and point me toward the right solution, I'd be greatful

  1.  
  2. // begin program
  3.  
  4. #include <iostream> // required to perform C++ stream I/O
  5. #include <cstdlib> // INCLUDE standard library to call external files w/ one parameter
  6. #include <string> // to have the ability to deal with strings
  7. #include <iomanip> // required for parameterized stream manipulation
  8.  
  9. using namespace std; // for accessing C++ Standard Library members
  10.  
  11. int main()
  12. {
  13.  
  14. // define variables
  15. string printShare;
  16. string workStation;
  17. string parameters;
  18.  
  19. cout << "\nWelcome to the Global Add Printer Script!";
  20. cout << "\nPlease Enter the NetBIOS/share name";
  21. cout << "\nof the workstation you're adding a printer to.";
  22. cout << "\nFor example: WS-4SR-VS-08A/printername";
  23. cout << "\n\nENTER PRINTER NAME: ";
  24. getline( cin, printShare); // store user input in string form
  25.  
  26. if ( printShare.size() == 0 )
  27. {
  28. cout << "\nError: Please enter a share name." << endl;
  29. } // end if
  30. else
  31. {
  32.  
  33. cout << "\nEnter this computer's";
  34. cout << "\nfull name: ";
  35. getline( cin, workStation);
  36.  
  37. }
  38.  
  39. if ( workStation.size() == 0 )
  40. {
  41. cout << "\nError: Please enter a computer name." << endl;
  42. } // end if
  43.  
  44. else
  45. { parameters = (workStation, printShare);
  46.  
  47. string s = "apwp.cmd ";
  48.  
  49.  
  50. system ( ( s + parameters ).c_str() );
  51.  
  52. } // end else
  53.  
  54. return 0; // indicate program is successful
  55.  
  56. } // End main

Here's what the batch file looks like:

  1. rundll32 printui.dll,PrintUIEntry /ga /c\\%1 /n\\%2
  2. sc \\%1 stop spooler
  3. sc \\%1 start spooler

I get the following result

http://linuxblog.sytes.net/downloads/docs/errorCpp.gif


Notice that after /n where %2 (second parameter) is supposed to go we find nothing passed to the batch script...or at least it looks that way. So, the important question is...was I correct using the following code?:

  1. parameters = (workStation, printShare);

Thanks again for humoring me...I'm only in the first 9 weeks of learning how to code this stuff.
My Home Away from Home: Yet Another Linux Blog
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Calling external files

 
0
  #6
May 3rd, 2005
>parameters = (workStation, printShare);
This doesn't work like you think it does. The comma operator in C++ will evaluate each part, then return the last item. So that line of code is equivalent to tis:
  1. workStation; // Do nothing
  2. parameters = printShare;
If you want to add the two strings together and assign them to parameters, do just that:
  1. parameters = workStation + ' ' + printShare;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: JacenSolo is an unknown quantity at this point 
Solved Threads: 0
JacenSolo JacenSolo is offline Offline
Newbie Poster

Re: Calling external files

 
0
  #7
Aug 18th, 2008
I'm looking to include a .mov file, not a batch file, but I can't figure that out...is there anyone who can help?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Calling external files

 
0
  #8
Aug 18th, 2008
Don't resurrect old threads!
How about beginning with real basic stuff like being able to tell the time, and read intro threads.
Then in a few years we can move on to the hard stuff like batch files.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC