c++ - I found a new paradigm: system -
i learning how process command line arguments. have program needs call program, both written in c++. have windows computer. program
#include "stdafx.h" #include <iostream> #include <sstream> using namespace std; void main() { char buffer[200]; char arg1[6]="hello"; std::stringstream buffer; sprintf(buffer, "c:\system.exe %i %i", arg1); system(buffer); system("pause"); } i need to call following program:
#include "stdafx.h" #include <iostream> #include <iomanip> #include <string> using namespace std; void main(int argc, char*argv[]) { string baci; for(int = 1; < argc; i++) baci += argv[i]; if (baci=="hello") cout << "francesco, ti mando 4 baci !!!" << endl; system("pause"); } it's not creating command line correctly , can't figure out why. can , explain variation needed use double argument system executable ? books , net not detailed procedure. found decent unable use purposes. c++: how use command line arguments
aside code not compiling there couple of glaring problems. first there no such thing void main() in c++. must return int. second problem redeclaring buffer std::stringstream. third issue fact passing value first argument sprintf not work since it's neither char* or being passed pointer. forth problem trying expand argument integer value (%i) instead of c style string (%s). have 2 format specifiers arguments pass one. result of undefined , second %i can expand value.
your code should this...
int main() { char buffer[200]; char arg1[6]="hello"; sprintf(buffer, "c:\\system.exe %s", arg1); system(buffer); system("pause"); return 0; } to pass double argument change print specifier in format string.
double arg1 = 1.0f; sprintf(buffer, "c:\\system.exe %lf", arg1); to pass more 1 argument need include additional format specifier , pass argument sprintf
sprintf(buffer, "c:\\system.exe %s %s %i", arg1, arg2string, arg3int); your second program has problem , don't think it's going execute way expect. thomas pointed out in comment concatenating arguments , doing comparison. if execute system.exe hello there baci hello there seems incorrect. since want see if hello has been sent should check in loop instead of concatenating string.
void main(int argc, char*argv[]) { for(int = 1; < argc; i++) if (0 == strcmp(argv[i], "hello")) cout << "francesco, ti mando 4 baci !!!" << endl; system("pause"); }
Comments
Post a Comment