| | |
Calling external files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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?
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.
My Home Away from Home: Yet Another Linux Blog
The only standard method is the system function:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> int main ( int argc, char *argv[] ) { if ( argc < 2 ) { std::cerr<<"Too few arguments"<<std::endl; return EXIT_FAILURE; } // Be ultra-anal if ( std::system ( 0 ) == 0 ) { std::cerr<<"No command processor available"<<std::endl; return EXIT_FAILURE; } return std::system ( argv[1] ); }
I'm here to prove you wrong.
Ok...so forgive my silliness here...I've found something from the tutorials here at daniweb
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!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> // You need this to call the system() function using namespace std; int main() { char prog[100]; cout << "Enter the program you want to call:\t"; // Prompt for a program and run it. cin.getline(prog); system(prog); 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
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { string s = "mybatchfile "; string input; cout<<"Parameters: "; getline ( cin, input ); system ( ( s + input ).c_str() ); }
I'm here to prove you wrong.
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
Here's what the batch file looks like:
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?:
Thanks again for humoring me...I'm only in the first 9 weeks of learning how to code this stuff.
C++ Syntax (Toggle Plain Text)
// begin program #include <iostream> // required to perform C++ stream I/O #include <cstdlib> // INCLUDE standard library to call external files w/ one parameter #include <string> // to have the ability to deal with strings #include <iomanip> // required for parameterized stream manipulation using namespace std; // for accessing C++ Standard Library members int main() { // define variables string printShare; string workStation; string parameters; cout << "\nWelcome to the Global Add Printer Script!"; cout << "\nPlease Enter the NetBIOS/share name"; cout << "\nof the workstation you're adding a printer to."; cout << "\nFor example: WS-4SR-VS-08A/printername"; cout << "\n\nENTER PRINTER NAME: "; getline( cin, printShare); // store user input in string form if ( printShare.size() == 0 ) { cout << "\nError: Please enter a share name." << endl; } // end if else { cout << "\nEnter this computer's"; cout << "\nfull name: "; getline( cin, workStation); } if ( workStation.size() == 0 ) { cout << "\nError: Please enter a computer name." << endl; } // end if else { parameters = (workStation, printShare); string s = "apwp.cmd "; system ( ( s + parameters ).c_str() ); } // end else return 0; // indicate program is successful } // End main
Here's what the batch file looks like:
C++ Syntax (Toggle Plain Text)
rundll32 printui.dll,PrintUIEntry /ga /c\\%1 /n\\%2 sc \\%1 stop spooler 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)
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
>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:
If you want to add the two strings together and assign them to parameters, do just that:
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)
workStation; // Do nothing parameters = printShare;
C++ Syntax (Toggle Plain Text)
parameters = workStation + ' ' + printShare;
I'm here to prove you wrong.
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.
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.
![]() |
Similar Threads
- Can't open any xml file (PHP)
- kb preloader for dynamic files (Graphics and Multimedia)
- reading external files into java (Java)
- Using data i read from *.* files (C++)
Other Threads in the C++ Forum
- Previous Thread: Make Form2 the startingForm
- Next Thread: "Form1.Form7.resources"
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






