943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9314
  • C++ RSS
Apr 19th, 2005
0

Calling external files

Expand Post »
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.
Similar Threads
TKS
Reputation Points: 108
Solved Threads: 18
Posting Pro in Training
TKS is offline Offline
470 posts
since Jan 2004
Apr 19th, 2005
0

Re: Calling external files

The only standard method is the system function:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 3rd, 2005
0

Re: Calling external files

Ok...so forgive my silliness here...I've found something from the tutorials here at daniweb

C++ Syntax (Toggle Plain Text)
  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!
TKS
Reputation Points: 108
Solved Threads: 18
Posting Pro in Training
TKS is offline Offline
470 posts
since Jan 2004
May 3rd, 2005
0

Re: Calling external files

C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 3rd, 2005
0

Re: Calling external files

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

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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?:

C++ Syntax (Toggle Plain Text)
  1. parameters = (workStation, printShare);

Thanks again for humoring me...I'm only in the first 9 weeks of learning how to code this stuff.
TKS
Reputation Points: 108
Solved Threads: 18
Posting Pro in Training
TKS is offline Offline
470 posts
since Jan 2004
May 3rd, 2005
0

Re: Calling external files

>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:
C++ Syntax (Toggle Plain Text)
  1. workStation; // Do nothing
  2. parameters = printShare;
If you want to add the two strings together and assign them to parameters, do just that:
C++ Syntax (Toggle Plain Text)
  1. parameters = workStation + ' ' + printShare;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 18th, 2008
0

Re: Calling external files

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JacenSolo is offline Offline
1 posts
since Aug 2008
Aug 18th, 2008
0

Re: Calling external files

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Make Form2 the startingForm
Next Thread in C++ Forum Timeline: "Form1.Form7.resources"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC