Standard Input/Output Streams

Slides:



Advertisements
Similar presentations
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
Advertisements

File streams Chapter , ,
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
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.
Chapter 3: Input/Output
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
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.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
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.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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.
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:
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
 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.
Chapter 3: Input/Output
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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.
1 Chapter 4 Program Input and the Software Design Process.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
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.
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Basic Input and Output Operations
Chapter 2 part #3 C++ Input / Output
Data Streams.
Input and Output Chapter 3.
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
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.
when need to keep permanently
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
Introduction to cout / cin
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Chapter 2 part #3 C++ Input / Output
Input/Output Streams, Part 1
Input/Output Streams, Part 2
Presentation transcript:

Standard Input/Output Streams A stream is a sequence of characters Streams convert internal representations to character streams or vice versa >> input (extraction) operator << output (insertion) operator Any character (printable or not) A stream has no fixed size

cin and cout Defined in the iostream library cin cout #include <iostream> cin Standard input stream Accepts typed input from the keyboard Uses the extraction operator >> cout Standard output stream Outputs to the terminal window Uses the insertion operator << Note insertion and extraction operators “point” in the direction of data flow!

Input Stream, >> Operator Last character read is tracked with an input stream buffer pointer Each new input attempt begins at current pointer position Leading white space (blanks, tab, nwln) skipped until first non-white- space character is located, then… For strings, characters are read into string variable until white space is encountered For numeric value, all characters that are part of the numeric value are processed until a character that’s not legally part of a C++ number is read.

Output Stream - Manipulators setw Set the width of the next output value. After output the value resets to zero. Zero width defaults to the object width (i.e. no padding) boolalpha noboolalpha Switches between textual and numeric representation of booleans showpoint noshowpoint Controls whether decimal point is always included in floating-point representation skipws noskipws Controls whether leading whitespace is skipped on input left right Sets the placement of fill characters (i.e. right or left justification) fixed scientific Changes formatting used for floating-point I/O endl Outputs '\n' and flushes the output stream setprecision Changes floating-point precision Manipulators require the inclusion of the iomanip library - (except for setw) #include <iomanip>

External files Streams can be used to read and write from files Instead of cin and cout, file streams are defined cin/cout are automatically “attached” to the keyboard/terminal Using file streams First declare the stream variable Two types ifstream to read from a file ofstream to write to a file Use the stream variable as you would cin and cout i.e. with the insertion and extraction operators

Attaching to an External file Read or write data from/to a file rather than standard in or out Must include fstream library #include <fstream> Declare an input or output stream variable ifstream ins ofstream outs Open the file, using the file name (and path, if needed) ins.open(“input.txt”) outs.open(“output.txt”) Or: String inFile= “file.txt”; ins.open(inFile.c_str()); // Note: must convert to a “c-style” string cin and cout are stream variables that attach to the terminal instead of a file! Otherwise, they behave the same way

Example: Reading a File Name string fileName; ifstream ins; cout << “Enter the input file name: “; cin >> fileName; ins.open(fileName.c_str( )); Note: Need to use “C-style” string in the open() method c_str() method does the conversion from C++ string

Some stream methods fs.open(fname) fs.close() fs.eof() fs.fail() Open a file for input or output (attach to a file) fs.close() Disconnect a stream from an open file fs.eof() Tests for end-of-file condition. Returns true when the end of a file is reached (when the program attempts to read the <eof> character that marks the end of a file). fs.fail() Returns true is an operation fails to execute properly (ex: when a file open or an attempt to read input fails) fs.clear() Clears the error flag set when a failure occurs Note: fs is a stream (like ins/outs from the previous slides) – or cin/cout

More stream methods (for input) fs.get(ch) Extract the next character from the (input) stream and put it in the char variable ch fs.put(ch) Inserts (writes) the character ch into the (output) stream. getline(stream, string_variable, delimiter) Read up to a delimiter (see next slide) fs.ignore(num, delimiter) Ignore input up to delimiter (or num, whichever comes first) Note: fs is an input stream (like ins from the previous slides) – or cin

getline() method Usage: getline(stream, string_variable) Ex: getline(ins, name) // ins is an input stream, name is a string getline() reads up to the delimiter character Default delimiter is newline (c/r) Can specify delimiter as a third argument Ex: getline(ins, name, ‘#’) // ‘#’ is delimiter instead of newline Note: delimiter is extracted (removed) from the stream, but not stored getline() does not skip over leading white space (blanks, tab, nwln) If next character to read is delimiter, nothing gets read into the variable! But, can swallow delimiter with stream.ignore()…

Using ignore getline does not skip leading white space, so if a newline character is encountered at the beginning of the characters to be extracted, getline will stop immediately and won’t perform the expected task Use stream.ignore(num, delimiter) to swallow delimiter Ex: ins.ignore(100,’\n’) or cin.ignore(1000,’\n’); See example fileGetline.cpp