Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 121:509-512 Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.

Similar presentations


Presentation on theme: "CSCE 121:509-512 Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne."— Presentation transcript:

1 CSCE 121:509-512 Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

2 CSCE 121:509-512 Set 11: More Input and Output Outline Numeric Formatting File Modes Stringstreams Reading a Line Character Classification 2

3 CSCE 121:509-512 Set 11: More Input and Output Integer Formatting Options Integer values: – decimal (base 10), uses 0-9 Ex: 1234 = 1*10 3 + 2*10 2 + 3*10 1 + 4*10 0 – octal (base 8), uses 0-7 Ex: 2322 = 2*8 3 + 3*8 2 + 2*8 1 + 2*8 0 – hexadecimal (base 16), uses 0-9,a-f Ex: 4d2 = 4*16 2 + d*16 1 + 2*16 0 = 4*16 2 + 13*16 1 + 2*16 0 Use manipulators to tell input and output streams which base to use: –dec, oct, or hex – “sticky”: permanent until changed again 3

4 CSCE 121:509-512 Set 11: More Input and Output Integer Formatting Examples int x = 1234; cout << hex; // from now on, ints will be written to screen in hex: cout << x; cin >> oct; // from now on, ints will be read in from keyboard as // if they are in octal int y; cin >> y; cout << dec; // back to decimal output cin >> dec; // back to decimal input 4

5 CSCE 121:509-512 Set 11: More Input and Output Floating-Point Output Formatting Options general (only in std_lib_facilities_4.h): iostream chooses best format using n digits – Ex: 1234.57 – use manipulator general scientific: one digit before decimal point, n digits after decimal point, plus exponent – Ex: 1.2345678e+03 – use manipulator scientific fixed: n digits after decimal point, no exponent – Ex: 1234.567890 – use manipulator fixed n is the “precision” (coming up next) 5

6 CSCE 121:509-512 Set 11: More Input and Output Precision For general, precision is the number of digits For scientific, precision is number of digits after decimal point For fixed, precision is number of digits after decimal point Use setprecision manipulator (for output streams) – Ex: cout << scientific << setprecision(8); 6

7 CSCE 121:509-512 Set 11: More Input and Output Still More Formatting Options You can control the width (number of characters to be used for the output) – not sticky – output is never truncated to fit All this is the kind of stuff you look up when you need it 7

8 CSCE 121:509-512 Set 11: More Input and Output File Open Modes Alternatives: for reading: ios_base::in for writing: ios_base::out append: ios_base::app binary: ios_base::binary and more (more stuff to look up) Usage: ofstream of1(name1); // default is ios_base::out ifstream if1(name2); // default is ios_base::in ofstream ofs(name,ios_base::app); fstream fs(“myfile”,ios_base::in|ios_base::out); // read and write; actually is the default for fstream 8

9 CSCE 121:509-512 Set 11: More Input and Output Positioning in a filestream You can control where in the file you read and write –seekg (g for get, i.e., read) –seekp (p for put, i.e., write) BUT, whenever you can, use simple streaming – powerful abstraction – try to just use istream and ostream Positioning is far more error-prone – handling end of file is system-dependent 9

10 CSCE 121:509-512 Set 11: More Input and Output Text vs. Binary Files Text file is a sequence of characters (fixed-size units, each unit large enough to hold a char) – to delimit values we use separation/termination characters (e.g., space or tab) Binary file is a sequence of bits – we use sizes to delimit values (e.g., if we are reading an int followed by a double, there is no blank space in between) 10

11 CSCE 121:509-512 Set 11: More Input and Output Text vs. Binary Use text when you can – human-readable – easier to debug – portable across systems – most information can be represented reasonably Use binary when you must – e.g., image files, sound files – Look up how to do it when you need to do it 11

12 CSCE 121:509-512 Set 11: More Input and Output Stringstreams A stringstream – reads from a string (instead of a file or the keyboard) – writes to a string (instead of a file or the screen) double str_to_double(string s) { // try to convert characters in s to a floating-point value istringstream is(s); // input stringstream double d; is >> d; // access just like an istream if (!is) error(“double format error”); return d; } //... double d1 = str_to_double(“12.4”); double d2 = str_to_double(“1.34e-3”); double d3 = str_to_double(“twelve point three”); // error 12

13 CSCE 121:509-512 Set 11: More Input and Output Stringstreams Input stringstreams ( istringstream ) are useful for extracting typed objects out of strings Output stringstreams ( ostringstream ) are useful for formatting into a fixed-size space (e.g., a box in a GUI) – See textbook for ostringstream details; analogous to ostream, supports << 13

14 CSCE 121:509-512 Set 11: More Input and Output Line-Oriented Input Alternative approach to reading a series of values from the keyboard one at at time 1.read the entire line into a string string name; getline(cin,name); 2.convert the string into a stringstream istringstream ss(name); 3.extract the values from the istringstream ss >> first_name; ss >> second_name; 14

15 CSCE 121:509-512 Set 11: More Input and Output Getting All the Characters This way skips whitespace: char ch; while (cin >> ch) { if (isalpha(ch)) { /* do something */ } } This way lets you see every character: while (cin.get(ch)) { if (isspace(ch)) { /* do something */} else if (isalpha(ch)) {/* do something else*/} } 15

16 CSCE 121:509-512 Set 11: More Input and Output An Aside Useful functions that work on characters isspace(ch) // is ch whitespace? // (‘ ’, ‘\t\’, ‘\n’, etc.) isalpha(ch) // is ch a letter? (a-z, A-Z) isdigit(ch) // is ch a digit? (0-9) isupper(ch) // is ch an uppercase letter? islower(ch) // is ch a lowercase letter? isalnum(ch) // is ch a letter or a digit? etc. 16

17 CSCE 121:509-512 Set 11: More Input and Output Line-Oriented Input Advice Avoid line-oriented input when you can – i.e., use >> instead of getline() People often use getline() because they see no alternative – but it gets messy – you often end up using >> anyway to parse the values from a stringstream made out of the line – you often end up having to use get() to read individual characters 17

18 CSCE 121:509-512 Set 11: More Input and Output Acknowledgments Photo on slide 1: “Cables Close Up” by Michael Coghlan, licensed under CC BY-SA 2.0Cables Close Up Michael CoghlanCC BY-SA 2.0 Slides are based on those for the textbook: http://www.stroustrup.com/Programming/11_custom_io.ppt 18


Download ppt "CSCE 121:509-512 Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne."

Similar presentations


Ads by Google