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.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

File streams Chapter , ,
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
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.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
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 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
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.
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.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
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.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
Chapter 3: Input/Output
CSE1301 Computer Programming: Lecture 6 Input/Output.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
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.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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.
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.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
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.
Basic Input and Output Operations
Standard Input/Output Streams
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
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
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Dale/Weems/Headington Program Input and the Software Design Process
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Input/Output Streams, Part 1
File I/O in C++ I.
Introduction to Programming
getline() function with companion ignore()
Reading from and Writing to Files
Presentation transcript:

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  input vs. output, read vs. write  file; file name vs. file variable  open; close  end-of-file  conversion from character steam to C/C++ types

26 4/11/98 Stream I/O  C++ uses the concept of streams for I/O  both for keyboard/monitor and for files  C++ can also use old C-style printf, scanf, etc.  Mixing the two is not recommended  You must use only stream I/O in CSE143

27 4/11/98 What is a Stream?  Think of a stream as a sequence of characters  istream and ifstream are types of input streams  Keyboard ( istream )  File ( ifstream )  ostream and ofstream are types of output streams  Screen ( ostream )  File ( ofstream )

28 4/11/98 Streams in Pictures A stream is just a sequence of characters: Smythe, J someIstreamInstance Input cursor Welcome to someOStreamInstance Output cursor C++ Program input via >> operator output via << operator 64

29 4/11/98 Well-Known Streams  Global streams defined in iostream.h :  cin : standard input stream (usually keyboard)  cout : standard output stream (usually screen)  cerr : standard error stream  Programs can open other streams to/from files and other devices:  fstream.h : for input/output with files  strstream.h : for treating strings as streams

30 4/11/98 Stream Output Operation For output streams, << is the “put to” or "insertion" operator #include … int count = 23; cout << “Hello, World!” << endl; // endl equivalent to “\n” cout << “The count is ” << count << endl;

31 4/11/98 Stream Input Operation For input streams, >> is the “get from” or "extraction" operator #include … int x, ID; char Name[40]; cin >> x; cin >> Name >> ID; // Can read multiple items on one line // Note: no &’s as needed with scanf

32 4/11/98 How Stream Input Works Input stream characters are interpreted differently, depending on the data type: int ID; char Name[40]; char ch; cin >> ID; // interprets as integer cin >> ch; // reads a char, skipping whitespace cin >> Name; // interprets as character string, // skipping leading whitespace and //stopping at trailing whitespace

33 4/11/98 Other Stream Operations  Sometimes want to read from a file without ignoring whitespace (spaces, tabs, newlines) get(c);// reads one character into c  Get also returns a value which we can test to see if the operation succeeded. (It would fail, for instance, if the end-of-file was reached)  There are several varieties of get() and other I/O operations. See Appendix C and Lippman for details.

34 4/11/98 File Stream Example #include void main() { ifstream inFile("input.txt");// open input ofstream outfile("output.txt");// open output char ch; // should test for successful opening here.. while (inFile.get(ch)) {// while more input outFile.put(ch);// write it to output } inFile.close();// close the files... outFile.close(); }

35 4/11/98 Preview of C++ Classes  Streams are instances of C++ classes  Class instances are like structs in that they have fields (generally called members).  Members can be data or functions (methods)  We use the “.” syntax to access members. Eg: inFile.get(ch);// get a character outFile.put(ch);// put a character outFile.close();// close the stream outFile.eof();// end of File??

36 4/11/98 Stream Testing  We often want to test the status of a stream, to see if it has any more data.  It’s usually cleanest to use the return value of the input function … while (cin.get(ch)) {... } while (cin >> someInt) { // if the get succeeded // do something }

37 4/11/98 Stream Testing (2)  It’s also possible to use the eof() member function: if (!someFile.eof()) {... }  For keyboard input, special characters such as Ctrl-Z (on most PCs) or Ctrl-D (on UNIX) signal end-of-file

38 4/11/98 Stream Advantages  Type Safety  No more dangers of mismatched % -types scanf("%s", &someInt); // oops! scanf("%d", someInt); // dooh!  Readability  Easier to interpret than a sequence of % -types scanf("%d%c%d", &intOne, &oneChar, &intTwo); versus cin >> intOne >> oneChar >> intTwo;