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

Similar documents
Chapter 3 - Notes Input/Output

Input/Output Streams: Customizing

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

Software Design & Programming I

CPE Summer 2015 Exam I (150 pts) June 18, 2015

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

C++ Input/Output: Streams

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

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

Definition Matching (10 Points)

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

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

COMP322 - Introduction to C++

Scientific Programming in C V. Strings

CHAPTER 3 Expressions, Functions, Output

Lecture 3 The character, string data Types Files

Chapter 11 Customizing I/O

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

COMP322 - Introduction to C++

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

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

Chapter 11 Customizing I/O

C++ Programming Lecture 10 File Processing

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

C++ Input/Output Chapter 4 Topics

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

Chapter 11 Customizing I/O

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Chapter 10 Characters, Strings, and the string class

What we will learn about this week:

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

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

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

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

Getting started with C++ (Part 2)

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Full file at

Streams - Object input and output in C++

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

Setting Justification

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

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

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

Chapter 2. Outline. Simple C++ Programs

Objects and streams and files CS427: Elements of Software Engineering

Streams. Rupesh Nasre.

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

Engineering Problem Solving with C++, Etter/Ingber

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

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

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

Objectives. In this chapter, you will:

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

Chapter 3: Input/Output

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

C mini reference. 5 Binary numbers 12

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

Intermediate Programming / C and C++ CSCI 6610

Strings and Library Functions

Come and join us at WebLyceum

System Design and Programming II

UNIT- 3 Introduction to C++

Getting started with Java

The C++ Language. Arizona State University 1

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

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Chapter 12 - C++ Stream Input/Output

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

BITG 1233: Introduction to C++

Object Oriented Programming In C++

C Libraries. Bart Childs Complementary to the text(s)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

Chapter 8 - Characters and Strings

Chapter 2: Basic Elements of C++

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

String Processing CS 1111 Introduction to Programming Fall 2018

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

Chapter 10: Characters, C- Strings, and More About the string Class

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

Strings are actually 'objects' Strings

Visual C# Instructor s Manual Table of Contents

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

Week 5: Files and Streams

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

CS11 Advanced C++ Lecture 2 Fall

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

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

C: How to Program. Week /May/28

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Strings and Stream I/O

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Transcription:

Topic 2 1. Reading and writing text files 2. Reading text input 3. Writing text output 4. Parsing and formatting strings 5. Command line arguments 6. Random access and binary files

Reading Words and Characters What really happens when reading a string? string word; in_file >> word; 1. Any whitespace is skipped (whitespace is: '\t' '\n' ' '). 2. The first character that is not white space is added to the string word. More characters are added until either another white space character occurs, or the end of the file has been reached. You can read a single character, including whitespace, using get(): char ch; in_file.get(ch); The get method returns the not failed condition so: while (in_file.get(ch)) //reads entire file, char by char { // Process the character ch }

Lookahead: Reading a Number Only If It Is a Number You can look at a character after reading it and then put it back. This is called one-character lookahead. A typical usage: check for numbers before reading them so that a failed read won t happen: char ch; int n=0; //for reading an entire int in_file.get(ch); if (isdigit(ch)) // Is this a number? { // Put the digit back so that it will // be part of the number we read in_file.unget(); data >> n; // Read integer starting with ch }

Functions in <cctype> (Handy for Lookahead): Table 1 Function Accepted Characters isdigit 0... 9 isalpha islower isupper a... z, A... Z a... z A... Z isalnum a... z, A... Z, 0... 9 isspace White space (space, tab, newline, and the rarely used carriage return, form feed, and vertical tab)

Reading A Whole Line: getline The function getline() reads a whole line up to the next '\n', into a C++ string. The '\n' is then deleted, and NOT saved into the string. string line; ifstream in_file("myfile.txt"); getline(in_file, line); NOTE: to read a line into a C-string, the syntax is different. getline becomes a member function of the stream object: char cline[100]; in_file.getline(cline, 99); //the int argument // is the max number of chars to read

Reading A Whole Line in a Loop: getline The getline function, like the others we ve seen, returns the not failed condition. To process a whole file line by line: string line; while( getline(in_file, line)) //reads whole file { // Process line }

Processing a File Line by Line Here is a CIA file from http://www.cia.gov/library/publications/the-world-factbook/ Each line has: country name, its population, a newline character. (And there is some whitespace in there too.) China 1330044605 India 1147995898 United States 303824646... We'll read the file line by line with getline. To extract the data from a line string, we need to find out where the name ends and the number starts.

Parsing a File Line using <cctype> Functions // Locate the first digit of population int i = 0; while (!isdigit(line[i])) { i++; } // Go backwards to find end of country name int j = i - 1; while (isspace(line[j])) { j--; }

Capturing the Line characters into separate strings Finally, extract the country name and population: string country_name = line.substr(0, j + 1); string population = line.substr(i); There is just one problem: population is stored in a string, not a number. You will see in Section 8.4 how to extract the population number as an int

Common Error: Mixing >> and getline Input A problem occurs when a call to getline follows a >> >> does not remove any trailing '\n' from the input stream buffer, though it does skip a leading '\n' until it finds non-whitespace The getline reads only the newline left by the preceding >>, considering it as the end of an empty line. This would happen if the following code were used on a file that had lines of strings interleaved with lines of numbers: while(!in.fail()) { getline(in, country_name); //gets empty strings in >> population; }

The Fix for Mixing >> and getline Input To solve the problem, any trailing newline in the input buffer must be removed before calling getline We do this by adding a dummy getline after the >> statement: string dummy; while(!in.fail()) { getline(in, country_name); //gets empty strings in >> population; getline(in, dummy); //delete the dangling '\n' } There are other alternative remedies for deleting the dangling newline, including calling in.get() or in.ignore(), but we will leave those as research exercises for the reader.

Practice It: Line and Character Input ifstream in; string str; char ch; Write statements to carry out the following tasks (answers shown in tiny font): Set str to the next line in the input. getline(in, str); The getline function reads the next line from the stream and places it in the string str. The newline is not part of the result. Set str to the next word in the input. in >> str; The >> operator reads the next word from the stream, removing any whitespace that precedes it. Set ch to the next character in the input, skipping any whitespace. Set ch to the next character in the input, but do not skip whitespace. Fill in the condition to check whether ch is a digit: if (...) { num = ch - '0'; } in >> ch; in.get(ch); isdigit(ch) Use the >> to get the next character from the stream, skipping any whitespace. Use the get function to get the next character from the stream, even if it is whitespace. The cctype header has functions for testing whether a character is a digit, letter, or whitespace.

Topic 3 1. Reading and writing text files 2. Reading text input 3. Writing text output 4. Parsing and formatting strings 5. Command line arguments 6. Random access and binary files

Writing Text Output You use the operator >> to send strings and numbers to an output stream, and the put function for a single char: string name = "Hello"; int value = 2; char ch = '!'; ofstream out_file; out_file.open("output.txt"); if (out_file.fail()) { return -1; } out_file << name << " " << value << endl; out_file.put(ch);

Formatting Output Manipulators To control how the output is formatted, you use stream manipulators. A manipulator is an object that: is sent to a stream using the >> operator. affects the behavior of the stream.

Manipulators to Display a Time such as 09:01 Use setfill when you need to pad numbers with leading zeroes. To set the width in which to display, use setw: strm << setfill('0') << setw(2) << hours << ": << setw(2) << minutes << setfill(' '); That last setfill(' ') re-sets the fill back to the default space character.

Stream Manipulators: Table 2 Manipulator Purpose Example Output setw Sets the field width of the next item only. out_file << setw(6) << 123 << endl << 123 << endl << setw(6) << 12345678; 123 123 12345678 setfill Sets the fill character for padding a field. (default is a space.) out_file << setfill('0') << setw(6) << 123; 000123 left right Selects left alignment. Selects right alignment (default). out_file << left << setw(6) << 123; out_file << right << setw(6) << 123; 123 123 fixed Selects fixed format for floating-point numbers. double x = 123.4567; out_file << x << endl << fixed << x; 123.457 123.456700 setprecision Sets the number of significant digits for the default floating-point format, the number of digits after the decimal point for fixed format. double x = 123.4567; out_file << fixed << x << endl << setprecision(2) << x; 123.456700 123.46

Practice It: Formatting Output Produce this output to the ofstream strm (answers shown in tiny font): 12.345679 123456789.000000 strm << setprecision(6) << fixed << 12.3456789 << " " << 123456789.0; (column width is 10): 123 4567 strm << setw(10) << 123 << endl << setw(10) << 4567; ---------- ---------- Count: 177 ---------- ---------- strm << "---------- ----------\n" << left << setw(10) << "Count:" << right << setw(11) << 177 << "---------- ----------\n";

Floating Point Formats: fixed, scientific, defaultfloat For money values, choose fixed format with two digits after the decimal point. out_file << fixed << setprecision(2); To get the scientific format: a number with one digit before the decimal point and an exponent. As with the fixed format, the setprecision denotes the number of digits after the decimal point. : out_file << scientific << setprecision(3) << 123.456; produces 1.235e+02 To switch back to the default floating-point format, use the defaultfloat manipulator introduced in C++ 11: out_file << defaultfloat;

Unicode, UTF-8, and C++ Strings The Unicode standard encodes alphabets from many languages, and some icons. Each Unicode character is 21-bits, written in hexadecimal for humans viewing the code For example, é (Latin small letter e with acute accent) has the code U+OOE9 And (high speed train) has the code U+1F684. For efficiency, characters in files or transmitted over the Internet are saved at less than 21 bits. each Unicode character saved as a sequence of one or more bytes. In your programs, you can use the \U prefix followed by 8 hex digits for such characters: string e_acute = u8"\u000000e9"; string high_speed_train = u8"\u0001f684"; Use the find member function to look for a Unicode character: string message =...; size_t pos = message.find(e_acute); if (pos!= string::npos) { // Message has e_acute starting at position pos } The size_t type indicates a non-negative position, and string::npos is a special value that denotes no position.