Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.

Similar presentations


Presentation on theme: "CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions."— Presentation transcript:

1 CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions too unsigned int, unsigned long, etc. –C++ guarantees a char is one byte in size –Sizes of other types are platform dependent –Can determine using sizeof(), INT_MAX float, double (floating point division) –More expensive in space and time –Useful when you need to describe continuous quantities bool –Logic type, takes on values true, false enumerations enum primary_colors {red, blue, yellow}; Exercise: enumerate truth, character, string, decimal

2 CSE 232: C++ Input/Output Manipulation C-style Strings Arrays of characters “hello, world!” // 14 positions w/ ‘\0’ char greeting[6] = “hello”; char *audience = “world”; Can reference specific positions as characters –Useful for some input checking tasks isalnum (greeting[i]) // array syntax isdigit (audience + i) // pointer syntax Can also reference as an entire string –Using functions found in the library if (strcmp (s, “hello”) == 0){...} if (strlen (s) == 1){...} Exercise: check argv[1] for “true” or “false” Exercise: check argv[1] for single character input

3 CSE 232: C++ Input/Output Manipulation Program Argument Checking Character based checking –There are also useful functions in the library –See page 249 of Prata C++ Primer Plus, 5 th Ed. isdigit() // in ‘0’to ‘9’(decimal digit) isxdigit() // in ‘0’to ‘9’, ‘A’ to ‘F’, // or ‘a’ to ‘f’ (hexadecimal digit) isalpha() // in ‘A’ to ‘Z’ or ‘a’ to ‘z’ (letter) islower() // in ‘a’ to ‘z’ (lowercase letter) isupper() // in ‘A’ to ‘Z’ (uppercase letter) isalnum() // alphanumeric (letter or decimal digit) ispunct() // punctuation character isblank() // blank (space or horizontal tab) Exercise: check whether any of the strings passed by argv represent unsigned decimal integers –we’ll allow leading zeroes but not a + or – –if so, print them on separate lines using cout

4 CSE 232: C++ Input/Output Manipulation C++ string Class #include using namespace std; int main (int, char*[]) { string s; // empty s = “”; // empty s = “hello”; s += “, ”; s = s + “world!”; cout << s << endl; return 0; } header file Various constructors Assignment operator Overloaded operators += + = == [] The last one is really useful: indexes string if (s[0] == ‘h’) …

5 CSE 232: C++ Input/Output Manipulation Using C++ vs. C-style Strings #include #include // strcmp... using namespace std; int main (int, char*[]) { char * w = “world”; string sw = “world”; char * h = “hello, ”; string sh = “hello, ”; cout << h << endl; cout << sh << endl; sh += sw; // cannot say h += w; cout << sh << endl; return 0; } C-style strings are contiguous arrays of char –Often accessed through pointers to char ( char * ) C++ string class (template) provides a rich set of overloaded operators Often C++ strings do “what you would expect” as a programmer Often C-style strings do “what you would expect” as a machine designer In this course we’ll focus on the programmer role

6 CSE 232: C++ Input/Output Manipulation C++ Input/Output Stream Classes #include using namespace std; int main (int, char*[]) { int i; // cout == std ostream cout << “how many?” << endl; // cin == std istream cin >> i; cout << “You said ” << i << “.” << endl; return 0; } header file –Use istream for input –Use ostream for output Overloaded operators << ostream insertion operator >> istream extraction operator Other methods –ostream: write, put –istream: get, eof, good, clear Stream manipulators –ostream: flush, endl, setwidth, setprecision, hex, boolalpha

7 CSE 232: C++ Input/Output Manipulation C++ File I/O Stream Classes #include using namespace std; int main () { ifstream ifs; ifs.open (“in.txt”); ofstream ofs (“out.txt”); if (ifs.is_open () && ofs.is_open ()) { int i; ifs >> i; ofs << i; } ifs.close (); ofs.close (); return 0; } header file –Use ifstream for input –Use ofstream for output Other methods –open, is_open, close –getline –seekg, seekp File modes –in, out, ate, app, trunc, binary

8 CSE 232: C++ Input/Output Manipulation C++ String Stream Classes #include using namespace std; int main () { ifstream ifs (“in.txt”); if (ifs.is_open ()) { string line_1, word_1; getline (ifs, line_1); istringstream iss (line_1); iss >> word_1; cout << word_1 << endl; } return 0; } header file –Use istringstream for input –Use ostringstream for output Useful for scanning input –Get a line from file into string –Wrap string in a stream –Pull words off the stream Useful for formatting output –Use string as format buffer –Wrap string in a stream –Push formatted values into stream –Output formatted string to file

9 CSE 232: C++ Input/Output Manipulation Using C++ String Stream Classes #include using namespace std; int main (int argc, char *argv[]) { if (argc < 3) return 1; ostringstream argsout; argsout << argv[1] << “ ” << argv[2]; istringstream argsin (argsout.str()); float f,g; argsin >> f; argsin >> g; cout << f << “ / ” << g << “ is ” << f/g << endl; return 0; } Program gets arguments as C-style strings But let’s say we wanted to input floating point values from the command line Formatting is tedious and error-prone in C-style strings ( sprintf etc.) iostream formatting is friendly Exercise: check whether any of the strings passed by argv are unsigned decimal integers (leading zeroes still ok) –print their sum if there are any –otherwise print the value 0


Download ppt "CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions."

Similar presentations


Ads by Google