Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.

Slides:



Advertisements
Similar presentations
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Advertisements

File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 File I/O 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.
Chapter 3: Input/Output
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.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
1 CS161 Introduction to Computer Science Topic #13.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
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.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 3: Input/Output
1 Character Strings (Cstrings) Reference: CS215 textbook pages
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
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.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Program Input and the Software Design Process ROBERT REAVES.
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.
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.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
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.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Basic Input and Output Operations
(2-1) Data Structures & The Basics of a Linked List I
Chapter 2 part #3 C++ Input / Output
Data Streams.
(2-1) Data Structures & The Basics of a Linked List I
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Chapter 3: Input/Output
File I/O.
CHAPTER 4 File Processing.
Chapter 2 part #3 C++ Input / Output
Presentation transcript:

Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University

What is a Stream? A Refined Definition A sequence of objects (generally just considered bytes) that flow from a device to memory or from memory to a device For input operations, the bytes flow from the device (i.e. keyboard, network connection, disk, etc.) to main memory For output operations, the bytes flow from main menu to the device (screen, printer, etc.) C. Hundhausen, A. O’Fallon 2

Analogy for a Stream A conveyer belt – You can place an item in sequence on the belt, i.e. into the stream (insertion or output operation) – You can remove an item in sequence from the belt, i.e. take from the stream (extraction or input operation) C. Hundhausen, A. O’Fallon 3

Classic Streams vs. Standard Streams The classic input/output streams for C++ supported byte-sized char s, which represented the ASCII characters Many alphabets require more characters than can be represented by a byte and the ASCII character set does not provide the characters – The Unicode character set provides these ones C++ provides standard stream libraries to process Unicode characters ( wchar_t ) C. Hundhausen, A. O’Fallon 4

Standard Streams in C++ (1) For standard input/output streams, include – cin is a predefined object of class istream and is connected to the standard input device (i.e. keyboard) cin >> var // cin applying stream extraction operator – stops at whitespace for strings – cout is a predefined object of class ostream and is connected to the standard output device (i.e. screen) cout << var // cout applying stream insertion operator C. Hundhausen, A. O’Fallon 5

Standard Streams in C++ (2) Member function getline() will read a line from the stream – Inserts a null character at the end of the array of characters, removes and discards the ‘\n’ from the stream (i.e. stored as a C string) C. Hundhausen, A. O’Fallon 6

7 Recall the File Processing Algorithm! Step 1: open the desired file – Opening is based on filename and permissions (read, write, or append) – Associates a file with a stream Step 2: process the file – Read data from the file Does not affect file – Write data to the file Completely overwrites existing file – Add data to the end of the file Retains previous information in file Step 3: close the file – Disassociates a file from a stream

C. Hundhausen, A. O’Fallon 8 Files Streams in C++ (1) For input/output streams to work with files, include – ifstream objects enable input from a file – ofstream objects enable output to a file – fstream objects for input from and output to a file Associate file with a file stream either during construction (applying the constructor or by calling open() ) – fstream fstr(“filename.txt”) // an instantiation of fstream object or fstr.open(“filename.txt”) // after instantiation

Files Streams in C++ (2) Read from files using: – fstr >> var; // applying the stream extraction operator – stops at whitespace for strings – fstr.getline () // to read entire line into a character array Stored as a C string Write to files using: – fstr << var; // applying the stream insertion operator C. Hundhausen, A. O’Fallon 9

Files Streams in C++ (3) Each file ends with an end-of-file marker (EOF) – check if at end of file using fstr.eof() Close a file using: – fstr.close(); C. Hundhausen, A. O’Fallon 10

C. Hundhausen, A. O’Fallon 11 Closing Thoughts on Files Files are required for many applications Files may be created and manipulated in any manner appropriate for an application

C. Hundhausen, A. O’Fallon 12 References P.J. Deitel & H.M. Deitel, C++: How to Program (9th ed.), Prentice Hall, 2014 J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016

C. Hundhausen, A. O’Fallon 13 Collaborators Chris Hundhausen