1 / 11

CS 1704

CS 1704. Introduction to Data Structures and Software Engineering. Command Line Parameters. main(int argc, char* argv[])

orien
Download Presentation

CS 1704

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 1704 Introduction to Data Structures and Software Engineering

  2. 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.

  3. 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

  4. 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; }

  5. 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.

  6. 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?

  7. 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.

  8. istringstream example #include<iostream> #include<sstream> usingnamespace std; int main() { int a; string s = "456"; istringstream sin(s); sin >> a; cout << a << endl; return0; }

  9. 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.

  10. 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; }

  11. 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; }

More Related