計算機概論實習 2007-03-09. 2 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Object Oriented Programming in C++ IO Streams Kolkata, July 25, 2005.
Advertisements

File streams Chapter , ,
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
CS161 Introduction to Computer Science Character Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory 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.
C++ Programming Certificate University of Washington Cliff Green
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - C++ Stream Input/Output Outline 12.1 Introduction 12.2 Streams Classic Streams vs.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
File I/O ifstreams and ofstreams. Some istream Operations istream function Description cin >> ch; Extract next non-whitespace character from cin and store.
1 Streams 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.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 15 Strings as Character Arrays
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.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
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.
Chapter 3: Expressions and Interactivity.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Basic Input and Output Operations
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Working with Data Files
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.
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Chapter 12 - C++ Stream Input/Output
Chapter 12: Advanced File Operations.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Chapter 12 - C++ Stream Input/Output
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Chapter 1 c++ structure C++ Input / Output
Input/Output Streams, Part 1
Presentation transcript:

計算機概論實習

2 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer, etc.) in c scanf("%c", &ch); in c++ cin >> ch; in c printf("%c", ch); in c++ cout << ch; byte stream Computer

3 iostream Library Header Files iostream library Has header files with hundreds of I/O capabilities Standard input (cin) Standard output (cout) Unbuffered error (cerr) Buffered error (clog) Formatted I/O with parameterized stream manipulators File processing operations

4 Standard input (cin) Format input cin >> grade Need to include iostream and std:cin Using >> operator Normally skips whitespace (blanks, tabs, newlines) Can change this Returns 0 when EOF encountered Otherwise, returns reference to object State bits set if errors occur Discussed in 12.7 and 12.8

5 Standard output (cout) Format input cout << grade Need to include iostream and std:cout Using << operator

6 Exmaple Code #include using namespace std; void main(){ int i; cin >> i; cout << i; }

7 Multi-Input/Output int main(){ int a, b; cout << “Input two number: ”; cin >> a >> b; cout << “The inputs are ” << a << “ and ” << b; return 0; } Input two number: 4 7 The inputs are 4 and 7 space

8 Member Function: cin.get() get(charRef) With character reference argument Gets only one character, stores in charRef Returns reference to istream If EOF, returns -1 get(charArray, size) get(charArray, size, delimiter) Reads until size-1 characters read, or delimiter encountered Default delimiter '\n' Delimiter stays in input stream Can remove with cin.get() or cin.ignore() Makes array null-terminated

9 Member Function: cin.getline() getline(array, size) getline(array, size, delimiter) Reads size-1 characters, or until delimiter found Default delimiter '\n' Removes delimiter from input stream Puts null character at end of array

10 What Difference Among cin, cin.get(), and cin.getline() (1/3) int i; cin >> i; cout << “i = ” << i; 456\n 456 i = 456 \n skip due to >> \n

11 What Difference Among cin, cin.get(), and cin.getline() (2/3) char ch[10]; cin.get(ch, 10); cout << ch; 456\n 456 ch[0] = ‘4’ ch[1] = ‘5’ ch[2] = ‘6’ ch[3] = 0 ch[0] = 0 56\n 6 Delimiter stays in input stream Nothing is read

12 What Difference Among cin, get(), and getline() (3/3) char ch[10]; cin.getline(ch, 10); cout << ch; 456\n 456 ch[0] = ‘4’ ch[1] = ‘5’ ch[2] = ‘6’ ch[3] = 0 ch[0] = 0 56\n Removes delimiter from input stream

13 Throw Away Delimiter If use cin.get(), the delimiter will stay in the stream. Using the following method to Throw it Away. int main() { char buf[100]; char trash; while(cin.get(buf,100)) { // Get chars until ‘\n’ cin.get(trash); // Throw away the terminator cout << buf <<'\n'; // Add the ‘\n’ at the end } return 0; } Input : This is a test Output: This is a test

14 Stream Error States Test state of stream using bits eofbit set when EOF encountered Function eof returns true if eofbit set cin.eof() failbit set when error occurs in stream Data not lost, error recoverable Function fail() returns true if set

15 Stream Error States badbit set when data lost Usually nonrecoverable Function bad() goodbit set when badbit, failbit and eofbit off Function good()

16 Stream Error States Member functions rdstate() Returns error state of stream Can test for goodbit, badbit, etc. Better to test using good(), bad() clear() Default argument goodbit Sets stream to "good" state, so I/O can continue Can pass other values cin.clear( ios::failbit ) Sets failbit Name "clear" seems strange, but correct

17 int main(){ int integerValue; // display results of cin functions cout << "Before a bad input operation:" << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << "\n\nExpects an integer, but enter a character: "; cin >> integerValue; // enter character value cout << endl;

18 // display results of cin functions after bad input cout << "After a bad input operation:" << "\ncin.rdstate(): " << cin.rdstate() << "\n cin.eof(): " << cin.eof() << "\n cin.fail(): " << cin.fail() << "\n cin.bad(): " << cin.bad() << "\n cin.good(): " << cin.good() << endl << endl; cin.clear(); // clear stream // display results of cin functions after clearing cin cout << "After cin.clear()" << "\ncin.fail(): " << cin.fail() << "\ncin.good(): " << cin.good() << endl; return 0; }

19 Before a bad input operation: cin.rdstate(): 0 cin.eof(): 0 cin.fail(): 0 cin.bad(): 0 cin.good(): 1 Expects an integer, but enter a character: c After a bad input operation: cin.rdstate(): 2 cin.eof(): 0 cin.fail(): 1 cin.bad(): 0 cin.good(): 0 After cin.clear() cin.fail(): 0 cin.good(): 1 Different error bit set leads to different value of rdstate()

20 Error Input Format – Simple Example int main () { int a; while (cin>>a){ cout<<"The input is:" <<a<<endl; } cout << "fail?" << cin.fail(); } 123 The input is:123 abc fail? 1

21 Error Input Format – Complex Example 123 The input is:123 ab fail?1 ab1 fail?1 123b The input is:123 fail?

22 Explanation of Example - 1 If there are not any error input, then program can get value from cin stream. while (cin>>a) 123 a=123

23 Explanation of Example - 2 When the input appears format error, the failbit set. In other words, the return value of cin.fail() is 1. But, the cin stream still keep the input value. For example, when you input ab, it will be kept in the stream. while (cin>>a) ab ab a = cin.fail() = 1

24 Explanation of Example - 4 When you input 1ab, a will be 1 but the ab will be kept in the cin stream. When you try to get from cin stream again, the failbit set. while (cin>>a) ab 1ab a=1 cin.fail() = 0 a= cin.fail() = 1 First time Second time

25 Error Input Format – Another Example Reset cin state and clear the stream. Then to request user to re-input with correct format (ex. Integer).

26 Example Program // in order to prevent user from inputting incorrect format, clear stream void clear2eol(){ cin.ignore(numeric_limits ::max(), '\n'); } int main(){ int a; while (cout > a)) { cin.clear(); // reset cin iostate as goodbit clear2eol(); // clear the stream cout << "Wrong input. "; } clear2eol(); return 0; } Need include limits Note that this code can not check the error such as “23abc”

27 Practice 1 (P1) Try to write a program that request user to input the student’s information including name and score and output the information. Please check that if the input of score is correct. If the input is not correct, then to request the user to re-input until the program gets a correct one.