Chapter 11 Customizing I/O

Similar documents
Chapter 11 Customizing I/O

Chapter 11 Customizing I/O

Input/Output Streams: Customizing

C++ Input/Output: Streams

Week 3: File I/O and Formatting 3.7 Formatting Output

I/O Streams and Standard I/O Devices (cont d.)

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

COMP322 - Introduction to C++

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

CS11 Advanced C++ Lecture 2 Fall

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Chapter 3 - Notes Input/Output

Software Design & Programming I

C++ Programming Lecture 10 File Processing

Engineering Problem Solving with C++, Etter/Ingber

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

CS2141 Software Development using C/C++ Stream I/O

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Getting started with C++ (Part 2)

EP241 Computing Programming

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

UEE1303(1070) S 12 Object-Oriented Programming in C++

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Fall 2017 CISC/CMPE320 9/27/2017

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

CS490: Problem Solving in Computer Science Lecture 3: Input/Output

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

We will exclusively use streams for input and output of data. Intro Programming in C++

Streams in C++ Stream concept. Reference information. Stream type declarations

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Chapter 3: Input/Output

Object Oriented Programming Using C++ UNIT-3 I/O Streams

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Setting Justification

Chapter 3 Objects, Types, and Values

Objects and streams and files CS427: Elements of Software Engineering

CS 103 Unit 14 - Streams

COMP322 - Introduction to C++

C++ Input/Output Chapter 4 Topics

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Streams. Ali Malik

Streams - Object input and output in C++

What we will learn about this week:

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Advanced I/O Concepts

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Chapter 2. Outline. Simple C++ Programs

Python Working with files. May 4, 2017

Streams. Rupesh Nasre.

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Chapter 12 - C++ Stream Input/Output

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

We can even use the operator << to chain the output request as:

10/23/02 21:20:33 IO_Examples

by Pearson Education, Inc. All Rights Reserved. 2

Expressions, Input, Output and Data Type Conversions

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CSCE 121 ENGR 112 List of Topics for Exam 1

Name Section: M/W T/TH Number Definition Matching (8 Points)

Writing a Good Program. 7. Stream I/O

CS 103 Unit 14 - Streams

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Java IO and C++ Streams

BITG 1233: Introduction to C++

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Fundamentals of Programming CS-110. Lecture 2

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Scientific Computing

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

C++ does not, as a part of the language, define how data are sent out and read into the program

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Chapter 6 Writing a Program

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Consider the following example where a base class has been derived by other two classes:

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Chapter 5 Errors. Bjarne Stroustrup

CS201 Solved MCQs.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Window s Visual Studio Output

UNIT- 3 Introduction to C++

The C++ Language. Arizona State University 1

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Name Section: M/W T/TH Number Definition Matching (6 Points)

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Transcription:

Chapter 11 Customizing I/O Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/~hkaiser/fall_2010/csc1253.html Slides adapted from: Bjarne Stroustrup, Programming Principles and Practice using C++

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Overview Input and output Numeric output Integer Floating point File modes Binary I/O Positioning String streams Line-oriented input Character input Character classification 2

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Kinds of I/O Individual values See Chapter 4, 10 Streams See Chapters 10-11 Graphics and GUI See Chapters 12-16 Text Type driven, formatted Line oriented Individual characters Numeric Integer Floating point User-defined types 3

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 123,456 mean? What does (123) mean? The world (of output formats) is weirder than you could possibly imagine 4

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Output formats Integer values 1234 2322 4d2 Floating point values 1234.57 1.2345678e+03 1234.567890 (decimal) (octal) (hexadecimal) (general) (scientific) (fixed) Precision (for floating-point values) 1234.57 (precision 6) 1234.6 (precision 5) Fields 12 (default for followed by 12 followed by ) 12 (12 in a field of 4 characters) 5

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Numerical Base Output You can change base Base 10 == decimal; digits: 0 1 2 3 4 5 6 7 8 9 Base 8 == octal; digits: 0 1 2 3 4 5 6 7 Base 16 == hexadecimal; digits: 0 1 2 3 4 5 6 7 8 9 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) 6

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Sticky Manipulators You can change base Base 10 == decimal; digits: 0 1 2 3 4 5 6 7 8 9 Base 8 == octal; digits: 0 1 2 3 4 5 6 7 Base 16 == hexadecimal; digits: 0 1 2 3 4 5 6 7 8 9 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: 1234 4d2 2322 2322 7

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Other Manipulators You can change base Base 10 == decimal; digits: 0 1 2 3 4 5 6 7 8 9 Base 8 == octal; digits: 0 1 2 3 4 5 6 7 Base 16 == hexadecimal; digits: 0 1 2 3 4 5 6 7 8 9 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: 1234 4d2 2322 1234 0x4d2 02322 8

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 << 1234.56789 << "\t\t(general)\n" // \t\t to line up columns << fixed << 1234.56789 << "\t(fixed)\n" << scientific << 1234.56789 << "\t(scientific)\n"; // results: 1234.57 (general) 1234.567890 (fixed) 1.234568e+003 (scientific) 9

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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.h scientific precision is the number of digits after the. (dot) fixed precision is the number of digits after the. (dot) // example: cout << 1234.56789 << '\t' << fixed << 1234.56789 << '\t' << scientific << 1234.56789 << '\n'; cout << general << setprecision(5) << 1234.56789 << '\t' << fixed << 1234.56789 << '\t' << scientific << 1234.56789 << '\n'; cout << general << setprecision(8) << 1234.56789 << '\t' << fixed << 1234.56789 << '\t' << scientific << 1234.56789 << '\n'; // results (note the rounding): 1234.57 1234.567890 1.234568e+003 1234.6 1234.56789 1.23457e+003 1234.5679 1234.56789000 1.23456789e+003 10

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 << 123456 <<' '<< setw(4) << 123456 << ' ' << setw(8) << 123456 << ' ' << 123456 << " \n"; cout << 1234.56 <<' '<< setw(4) << 1234.56 << ' ' << setw(8) << 1234.56 << ' ' << 1234.56 << " \n"; cout << "asdfgh" <<' '<< setw(4) << "asdfgh" << ' ' << setw(8) << "asdfgh" << ' ' << "asdfgh" << " \n"; // results: 123456 123456 123456 123456 1234.56 1234.56 1234.56 1234.56 asdfgh asdfgh asdfgh asdfgh 11

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 12

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 A file 0: 1: 2: 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 123.45 13

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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., add to the end of the file) ios_base::ate // at the 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 14

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Text vs. binary files 123 as characters: 1 2 3????? 12345 as characters: 1 2 3 4 5??? 123 as binary: 12345 as binary: 123 12345 In binary files, we use sizes to delimit values 123456 as characters: 123 456 as characters: 1 2 3 4 5 6? 1 2 3 4 5 6 In text files, we use separation/termination characters 15

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 16

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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); 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); if (!ofs) error("can't open output file ",name); // note: binary // note: binary // binary tells the stream not to try anything clever with the bytes 17

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Binary files vector<int> v; // read from binary file: int i; while (ifs.read(as_bytes(i),sizeof(int))) v.push_back(i); // do something with v // note: reading bytes } // write to binary file: for(int i=0; i<v.size(); ++i) ofs.write(as_bytes(v[i]),sizeof(int)); return 0; // note: writing bytes // for now, treat as_bytes() as a primitive 18

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Positioning in a filestream Put position: Get position: 2 6 0: 1: A file: y fstream fs(name.c_str()); // open for input and output // fs.seekg(5); // move reading position ( g for get ) to 5 (the 6 th character) char ch; fs>>ch; // read and increment reading position cout << "character[6] 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 19

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 20

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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() 21

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 String streams Very useful for formatting into a fixed-sized space (think GUI) for extracting typed objects out of a string 22

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Type vs. line Read a string string name; cin >> name; cout << name << '\n'; Read a line string name; getline(cin,name); // input: Dennis Ritchie // output: Dennis // input: Dennis Ritchie cout << name << '\n'; // output: Dennis Ritchie // now what? // maybe: istringstream ss(name); ss>>first_name; ss>>second_name; 23

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 } } 24

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Character classification functions If you use character input, you often need one or more of these (from header <cctype> ): isspace(c) isalpha(c) isdigit(c) isupper(c) islower(c) isalnum(c) // is c whitespace? (' ', '\t', '\n', etc.) // is c a letter? ('a'..'z', 'A'..'Z') note: not '_' // is c a decimal digit? ('0'.. '9') // is c an upper case letter? // is c a lower case letter? // is c a letter or a decimal digit? 25

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 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 often 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 26

CSC1253, Fall 2010, Lecture 12 October 5th, 2010 Next lecture Graphical output Creating a window Drawing graphs 27