110 likes | 305 Views
CS 1704. Introduction to Data Structures and Software Engineering. Command Line Parameters. main(int argc, char* argv[])
E N D
CS 1704 Introduction to Data Structures and Software Engineering
Command Line Parameters main(int argc, char* argv[]) When a program name is typed at the Operating System Line prompt, it is treated as a command. The name of the executable program file and any other strings typed on the line, (usually file names), are considered command line parameters. C/C++ compilers provide access to command line parameters.
Command Line Parameters Cont argc • gives the number of command line parameters (arguments) that the user typed on the command line. • always equals at least 1 for the command itself argv[] • an array of pointers to C-style strings that holds the command line arguments. • argv[0] holds the name of the program (command) • may hold full pathname on some systems • may be capitalized on DOS systems
Command Line Parameters Cont. #include <iostream> #include <cstdlib> // for EXIT_SUCCESS using namespace std; int main (int argc, char* argv[]) { cout << "argc = " << argc << endl; for (int i =0 ; i < argc; i++) { cout << "argv[ " << i << " ] = " << argv[i] << endl; } return EXIT_SUCCESS; }
Testing Switches (For P1) • From the command line: • C://checkers.exe 1 #include <iostream> using namespace std; int main (int argc, char* argv[]) { int x[4]; for (int i =0 ; i <= 4; i++) { if (argc > 0 && argv[1]==1) //See Note!! cout << x[i]; //DO SOME WORK ON THE ARRAY } return 1; } Note: Since argv is an array of char*’s, argv[1] in this case is not 1 but is “1” so you must use a conversion.
stringstreams • The stringstream is a class that is useful for extracting data from or writing formatted data to strings. • #include <sstream> • A very common question, for example is this: “How do I convert a string to a number?” What about a number to a string?
istringstream • The istringstream class is an input stream attached to a string. The constructor copies the string s into its private buffer, and then we may extract data from it using regular stream semantics.
istringstream example #include<iostream> #include<sstream> usingnamespace std; int main() { int a; string s = "456"; istringstream sin(s); sin >> a; cout << a << endl; return0; }
ostringstream • The ostringstream class is an output stream attached to a string. We add data to the string via the stream semantics and then can get the string with the .str() method.
ostringstream #include<sstream> #include<iostream> usingnamespace std; int main() { ostringstream str_out;int x = 42;str_out << "The answer to the question is " << 42 << endl; cout << str_out.str() << endl; str_out.str(""); str_out << 53.2; cout << str_out.str() << endl; return0; }
stringstream (both in and out) #include<sstream> #include<iostream> usingnamespace std; int main() {int val;string mystr; stringstream ss (stringstream::in | stringstream::out);ss << "120 42 377 6 5 2000“;for (int n=0; n < 6; n++) { ss >> val; cout << val*2 << endl;} return0; }