Chapter 11 Customizing I/O Bjarne Stroustrup www.stroustrup.com/Programming.

Slides:



Advertisements
Similar presentations
Chapter 10 Input/Output Streams
Advertisements

Chapter 11 Customizing I/O Bjarne Stroustrup
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 11 Customizing I/O Hartmut Kaiser
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Chapter 3: Input/Output
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Ch 10. Input/Output1 Ch. 10 Input/Output Oregon State University Timothy Budd.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Chapter 10 Input/Output Streams John Keyser’s Modifications of Slides by Bjarne Stroustrup
C++ Streams Lecture-2.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 10 Input/Output Streams Hartmut Kaiser
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
The C++ Programming Language Streams. Contents u Output Stream u Input Stream u Formatting u Manipulators u Files & Streams.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Chapter 3: Input/Output
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Chapter 05 (Part II) Control Statements: Part II.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 10 Input/Output Streams Hartmut Kaiser
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
ifstreams and ofstreams
Chapter 11 Customizing I/O
Basic Input and Output Operations
Chapter 11 Customizing I/O
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
File I/O Streams, files, strings 1 1.
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Programming with Data Files
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 11 Customizing I/O
Chapter 10 Input/Output Streams
Fundamental Programming
Chapter 11 Customizing I/O
ifstreams and ofstreams
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Programming Fundamental-1
Presentation transcript:

Chapter 11 Customizing I/O Bjarne Stroustrup

Overview Input and output Numeric output – Integer – Floating point File modes – Binary I/O – Positioning String streams Line-oriented input – Character input – Character classification 2Stroustrup/Programming

Kinds of I/O Individual values – See Chapters 4, 10 Streams – See Chapters Graphics and GUI – See Chapters Text – Type driven, formatted – Line oriented – Individual characters Numeric – Integer – Floating point – User-defined types 3Stroustrup/Programming

Observation As programmers we prefer regularity and simplicity – But, our job is to meet people’s expectations People are very fussy/particular/picky about the way their output looks – They often have good reasons to be – Convention/tradition rules What does 110 mean? What does 123,456 mean? What does (123) mean? – The world (of output formats) is weirder than you could possibly imagine 4Stroustrup/Programming

Output formats Integer values – 1234 (decimal) – 2322 (octal) – 4d2 (hexadecimal) Floating point values – (general) – e+03 (scientific) – (fixed) Precision (for floating-point values) – (precision 6) – (precision 5) Fields – |12| (default for | followed by 12 followed by |) – | 12| (12 in a field of 4 characters) 5Stroustrup/Programming

Numerical Base Output You can change “base” – Base 10 == decimal; digits: – Base 8 == octal; digits: – Base 16 == hexadecimal; digits: a b c d e f // simple test: cout << dec << 1234 << "\t(decimal)\n" << hex << 1234 << "\t(hexadecimal)\n" << oct << 1234 << "\t(octal)\n"; // The '\t' character is “tab” (short for “tabulation character”) // results: 1234(decimal) 4d2(hexadecimal) 2322(octal) 6Stroustrup/Programming

“Sticky” Manipulators You can change “base” – Base 10 == decimal; digits: – Base 8 == octal; digits: – Base 16 == hexadecimal; digits: a b c d e f // simple test: cout << 1234 << '\t' << hex << 1234 << '\t' << oct << 1234 << '\n'; cout << 1234 << '\n';// the octal base is still in effect // results: 12344d Stroustrup/Programming

Other Manipulators You can change “base” – Base 10 == decimal; digits: – Base 8 == octal; digits: – Base 16 == hexadecimal; digits: a b c d e f // simple test: cout << 1234 << '\t' << hex << 1234 << '\t' << oct << 1234 << endl;// '\n' cout << showbase << dec;// show bases cout << 1234 << '\t' << hex << 1234 << '\t' << oct << 1234 << '\n'; // results: 12344d x4d Stroustrup/Programming

Floating-point Manipulators You can change floating-point output format – general – iostream chooses best format using n digits (this is the default) – scientific – one digit before the decimal point plus exponent; n digits after. – fixed – no exponent; n digits after the decimal point // simple test: cout << << "\t\t(general)\n"// \t\t to line up columns << fixed << << "\t(fixed)\n" << scientific << << "\t(scientific)\n"; // results: (general) (fixed) e+03(scientific) 9Stroustrup/Programming

Precision Manipulator Precision (the default is 6) – general – precision is the number of digits Note: the general manipulator is not standard, just in std_lib_facilities_3.h – scientific – precision is the number of digits after the. (dot) – fixed – precision is the number of digits after the. (dot) // example: cout << << '\t' << fixed << << '\t' << scientific << << '\n'; cout << general << setprecision(5) << << '\t' << fixed << << '\t' << scientific << << '\n'; cout << general << setprecision(8) << << '\t' << fixed << << '\t' << scientific << << '\n'; // results (note the rounding): e e e+03 10Stroustrup/Programming

Output field width A width is the number of characters to be used for the next output operation – Beware: width applies to next output only (it doesn’t “stick” like precision, base, and floating-point format) – Beware: output is never truncated to fit into field (better a bad format than a bad value) // example: cout << <<'|'<< setw(4) << << '|' << setw(8) << << '|' << << "|\n"; cout << <<'|'<< setw(4) << << '|' << setw(8) << << '|' << << "|\n"; cout << "asdfgh" <<'|'<< setw(4) << "asdfgh" << '|' << setw(8) << "asdfgh" << '|' << "asdfgh" << "|\n"; // results: |123456| | | asdfgh|asdfgh| 11Stroustrup/Programming

Observation This kind of detail is what you need textbooks, manuals, references, online support, etc. for – You always forget some of the details when you need them 12Stroustrup/Programming

A file At the fundamental level, a file is a sequence of bytes numbered from 0 upwards Other notions can be supplied by programs that interpret a “file format” – For example, the 6 bytes "123.45" might be interpreted as the floating-point number :1:2: Stroustrup/Programming

File open modes By default, an ifstream opens its file for reading By default, an ofstream opens its file for writing. Alternatives: – ios_base::app // append (i.e., output adds to the end of the file) – ios_base::ate // “at end” (open and seek to end) – ios_base::binary // binary mode – beware of system specific behavior – ios_base::in // for reading – ios_base::out // for writing – ios_base::trunc // truncate file to 0-length A file mode is optionally specified after the name of the file: – ofstream of1(name1);// defaults to ios_base::out – ifstream if1(name2);// defaults to ios_base::in – ofstream ofs(name, ios_base::app); // append rather than overwrite – fstream fs("myfile", ios_base::in|ios_base::out); // both in and out 14Stroustrup/Programming

Text vs. binary files In binary files, we use sizes to delimit values In text files, we use separation/terminatio n characters ????? 12345??? as characters : as characters: as binary: 123 as binary : ? as characters: Stroustrup/Programming

Text vs. binary Use text when you can – You can read it (without a fancy program) – You can debug your programs more easily – Text is portable across different systems – Most information can be represented reasonably as text Use binary when you must – E.g. image files, sound files 16Stroustrup/Programming

Binary files int main()// use binary input and output { cout << "Please enter input file name\n"; string name; cin >> name; ifstream ifs(name.c_str(),ios_base::binary);// note: binary // c_str() not needed in C++11 if (!ifs) error("can't open input file ", name); cout << "Please enter output file name\n"; cin >> name; ofstream ofs(name.c_str(),ios_base::binary);// note: binary // c_str() not needed in C++11 if (!ofs) error("can't open output file ",name); // “binary” tells the stream not to try anything clever with the bytes 17Stroustrup/Programming

Binary files vector v; // read from binary file: int i; while (ifs.read(as_bytes(i),sizeof(int)))// note: reading bytes v.push_back(i); // … do something with v … // write to binary file: for(int i=0; i<v.size(); ++i) ofs.write(as_bytes(v[i]),sizeof(int));// note: writing bytes return 0; } // For now, treat as_bytes() as a primitive // Warning! Beware transferring between different systems 18Stroustrup/Programming

Positioning in a filestream fstream fs(name.c_str());// open for input and output // c_str() not needed in C++11 // … fs.seekg(5);// move reading position (‘g’ for ‘get’) to 5 (the 6 th character) char ch; fs>>ch;// read the x and increment the reading position to 6 cout << "sixth character is " << ch << '(' << int(ch) << ")\n"; fs.seekp(1);// move writing position (‘p’ for ‘put’) to 1 (the 2 nd character) fs<<'y';// write and increment writing position to 2 19 … y A file: 2 6 Put position: Get position: 0:1: Stroustrup/Programming 2:5:6:3:4: x

Positioning Whenever you can – Use simple streaming Streams/streaming is a very powerful metaphor Write most of your code in terms of “plain” istream and ostream – Positioning is far more error-prone Handling of the end of file position is system dependent and basically unchecked 20Stroustrup/Programming

String streams A stringstream reads/writes from/to a string rather than a file or a keyboard/screen double str_to_double(string s) // if possible, convert characters in s to floating-point value { istringstream is(s);// make a stream so that we can read from s double d; is >> d; if (!is) error("double format error"); return d; } double d1 = str_to_double("12.4");// testing double d2 = str_to_double("1.34e-3"); double d3 = str_to_double("twelve point three");// will call error() 21Stroustrup/Programming

String streams See textbook for ostringstream String streams are very useful for – formatting into a fixed-sized space (think GUI) – for extracting typed objects out of a string 22Stroustrup/Programming

Type vs. line Read a string string name; cin >> name;// input: Dennis Ritchie cout << name << '\n';// output: Dennis Read a line string name; getline(cin,name);// input: Dennis Ritchie cout << name << '\n';// output: Dennis Ritchie // now what? // maybe: istringstream ss(name); ss>>first_name; ss>>second_name; 23Stroustrup/Programming

Characters You can also read individual characters char ch; while (cin>>ch) {// read into ch, skipping whitespace characters if (isalpha(ch)) { // do something } while (cin.get(ch)) {// read into ch, don’t skip whitespace characters if (isspace(ch)) { // do something } else if (isalpha(ch)) { // do something else } 24Stroustrup/Programming

Character classification functions If you use character input, you often need one or more of these (from header ): – isspace(c)// is c whitespace? (' ', '\t', '\n', etc.) – isalpha(c)// is c a letter? ('a'..'z', 'A'..'Z') note: not '_' – isdigit(c)// is c a decimal digit? ('0'..'9') – isupper(c)// is c an upper case letter? – islower(c)// is c a lower case letter? – isalnum(c)// is c a letter or a decimal digit? etc. 25Stroustrup/Programming

Line-oriented input Prefer >> to getline() – i.e. avoid line-oriented input when you can People often use getline() because they see no alternative – But it easily gets messy – When trying to use getline(), you often end up using >> to parse the line from a stringstream using get() to read individual characters 26Stroustrup/Programming

Standardization What? Who? How? C++11 – -std=c++11 for GCC and Clang C++14 – It’s almost here Stroustrup/Programming27

C++14 Binary literals – 0b Digit separators – 0b1010'1001'0000'0011 – Also for decimal, octal, and hexadecimal User-Defined Literals (UDLs) in the standard library – Time: 2h+10m+12s+123ms+3456ns – Complex: 2+4i Manipulators – cout << defaultfloat << e-12;// what the book calls “general” – cout << hexfloat << e-12; Stroustrup/Programming28

Next lecture Graphical output – Creating a window – Drawing graphs 29Stroustrup/Programming