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

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

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

More About File Reading.

C++ Input/Output: Streams

Chapter 3 - Notes Input/Output

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

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

Software Design & Programming I

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

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

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

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

Definition Matching (10 Points)

Streams. Rupesh Nasre.

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

C++ Input/Output Chapter 4 Topics

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

CS103L SPRING 2018 UNIT 7: FILE I/O

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

Developed By : Ms. K. M. Sanghavi

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

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

CSCE 121 ENGR 112 List of Topics for Exam 1

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

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

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

PASS$MOCK$EXAM$ $FOR$PRACTICE$ONLY$

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

Writing a Good Program. 7. Stream I/O

LAB 4.1 Relational Operators and the if Statement

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

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

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

CS242 COMPUTER PROGRAMMING

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++

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

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

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

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

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

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

Introduction to C ++

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

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

This watermark does not appear in the registered version - Slide 1

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

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

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

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

Reading from files File errors Writing to files

1. In C++, reserved words are the same as predefined identifiers. a. True

Input/Output Streams: Customizing

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

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

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

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

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

What we will learn about this week:

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

Chapter 11 Customizing I/O

Chapter 7 Array. Array. C++, How to Program

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

File I/O Christian Schumacher, Info1 D-MAVT 2013

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

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

Chapter 12. Streams and File I/O

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

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Engineering Problem Solving with C++, Etter/Ingber

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

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Types of files Command line arguments File input and output functions Binary files Random access

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Introduction to Standard C++ Console I/O. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Objectives. In this chapter, you will:

Chapter 11 Customizing I/O

Sample Code: OUTPUT Daily Highs & Lows

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Week 5: Files and Streams

CSI33 Data Structures

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

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

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Objects and streams and files CS427: Elements of Software Engineering

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Transcription:

Class 14 Input File Streams

A program that reads 10 integers from a file int main() { string filename; // the name of the file to be opened ifstream fin; // the input file stream int array[10]; // an array to hold 10 integers cout << Enter file name << endl; cin >> filename; fin.open( filename.c_str() ); // unfortunately open will not take a string argument for ( int i = 0; i < 10; i++ ) // problem: if fin did not open, this will not do anything { fin >> array[i]; } }; fin.close();

When does fin.open fail? when the input file doesn t exist when you don t have read access to the file if file is already open by another program Test: if ( fin.fail() ) // this could also be if (!fin ) { cout << Could not open file << endl; }

Programming example Write a program that reads a file with 10 integers and writes them to another file The program should exit if either of the files fails to open

The input buffer When data is being read from a source, it is temporarily stored in an input buffer (array) This is true of cin as well as input file streams All input goes into this buffer, whether it is read in your program or not The input stream keeps track of the current position in the buffer. You can look at it with fin.peek() or cin.peek()

The extraction operator >> The extraction operator looks in the input buffer and 1) skips past white space (spaces, tabs, newline characters) 2) takes all characters that match the data type that is looking for 3) It stops when there is an inappropriate character for the data type If it can t extract any characters at all, the filestream fails. When it has failed, it will be unable to read data.

At what character does >> stop? character: the first whitespace after char (space, tab, newline) string: the first whitespace after string integer: the first non-digit after the int double/float: any character that isn t a digit or a decimal point (or if it reaches a second decimal point)

Example User inputs: 12 and then space, then 13, then tab, then 14, then enter twice Program: int numbers[3]; cin >> numbers[0] // line A >> numbers[1] // line B >> numbers[2]; // line C What happens in the input buffer?

Example: What happens in the input buffer? User enters: 123 123.4 123.45 Program: int ival; float fval; cin >> ival; cin >> ival; cin >> ival; cin >> fval; cin >> fval; // fails at this cin statement What happens: ival = 123 ival = 123 the next ival causes it to fail at the period

Fixing the input stream: clear If the input stream has failed, you can fix it by calling the clear function: cin.clear() or fin.clear() It is then necessary to get past the offending characters in the filestream.

Code: int cont; cout cin >> cont; Example: fix this code << Enter 1 to continue, 2 to stop << endl; User enters the character y and then enters a newline

Input stream function: ignore(int max, char delimiter) To move forward in the input stream, call cin.ignore( NUM, \n ); This will move past the first NUM characters, or to one character after the newline whichever happens first

Fixed code int cont; cout << please enter 1 to continue, 2 to stop cin >> cont; << endl; while ( cin.fail() ) { } cin.clear(); cin.ignore( 100, \n ); // ignores up to 100 characters cout << I did not understand your response: cin >> cont; << Please enter 1 to continue, 2 to stop << endl;

Example Write a program that gets integers from a file, input.dat It writes them to a second file, output.dat When: 1. a # is entered - this is written to output.dat 2. it fails to get an integer and there is no # it exits with a message