© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.

Slides:



Advertisements
Similar presentations
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Advertisements

CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
Chapter 9 Streams and File I/O Overview of Streams and File I/O
Chapter 10.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
HST 952 Computing for Biomedical Scientists Lecture 8.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Chapter 15.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
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.
Program Input and the Software Design Process ROBERT REAVES.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Lecturer: Dr. AJ Bieszczad Chapter 9 COMP 150: Introduction to Object-Oriented Programming 9-1 l Overview of Streams and File I/O l Text File I/O l Binary.
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 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 9 l Streams and Simple File I/O l Exception Handling with File.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
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.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
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.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
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
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
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.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
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.
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.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 14: Sequential Access Files
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Streams and File I/O.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
File I/O.
Streams and File I/O Chapter 14.
File I/O.
Interactive I/O Input from keyboard Must prompt user User friendly
CSS 161: Fundamentals of Computing
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Computer Programming with JAVA
File I/O in C++ I.
CHAPTER 4 File Processing.
Reading Data From and Writing Data To Text Files
File I/O in C++ I.
Presentation transcript:

© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O

© 2000 Scott S Albert I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file Output can be to display (screen) or a file Advantages of file I/O –permanent copy –output from one program can be input to another –input can be automated (rather than entered manually)

© 2000 Scott S Albert Streams Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as a buffer between the data source and destination Input stream: a stream that provides input to a program Output stream: a stream that accepts output from a program –cout is an output stream –cin is an input stream A stream connects a program to an I/O object –cout connects a program to the screen –cin connects a program to the keyboard

© 2000 Scott S Albert Binary versus text files All data and programs are ultimately just zeros and ones –each digit can have one of two values, hence binary –bit is one binary digit –byte is a group of eight bits Text files: the bits represent printable characters –one byte per character for ASCII, the most common code –for example, C++ source files are text files –so is any file created with a "text editor" Binary files: the bits represent other types of encoded information, such as executable instructions or numeric data –these files are easily read by the computer but not humans –they are not "printable" files actually, you can print them, but they will be unintelligible "printable" means "easily readable by humans when printed"

© 2000 Scott S Albert Opening a new output file The file name is given as a String –file name rules are determined by your operating system Opening an output file takes two steps 1. Create a ofstream object associated with the file name String # include 2. Use the open method to connect the file name

© 2000 Scott S Albert Example: opening an output file To open a file named numbers.dat : ofstream out_file; out_file.open(“numbers.dat”); To write to the file, Use out_file<<

© 2000 Scott S Albert Every file has two names The code to open the file creates two names for an output file –the name used by the operating system numbers.dat in the example –the stream name out_file in the example C++ programs use the stream name –out_file in the example

© 2000 Scott S Albert Closing a file An Output file should be closed when you are done writing to it Use the close method of the class o fstream For example, to close the file opened in the previous example: out_file.close(); If a program ends normally it will close any files that are open

© 2000 Scott S Albert If it is done automatically, why explicitly close files? If a program automatically closes files when it ends normally, why close them with explicit calls to close ? Two reasons: 1. To make sure it is closed if a program ends abnormally (it could get damaged if it is left open). 2. A file open for writing must be closed before it can be opened for reading. Although C++ does have a class that opens a file for both reading and writing, we will not discuss this topic

© 2000 Scott S Albert Warning: overwriting a file Opening a file creates an empty file Opening a file creates a new file if it does not already exist Opening a file that already exists eliminates the old file and creates a new, empty one –data in the original file is lost How to test for the existence of a file and avoid overwriting it will be covered later

© 2000 Scott S Albert Opening a new input file Similar to opening an output file, but replace "output" with "input" The file name is given as a String –file name rules are determined by your operating system Opening a file takes two steps 1. Creating a ifstream object associated with the file name String 2. Connecting the ifstream to a file name

© 2000 Scott S Albert Example: opening an input file To open a file named numbers.dat : ifstream in_file; infile.open(“numbers.dat”); To read from the file, Use in_file>>

© 2000 Scott S Albert Input file exceptions If (in_file.fail()) cout<<“Error opening file\n”; in>>x; while(!in.eof()) { cout<<x<<endl; in>>x; }

Example: reading a file name from the keyboard FileNameDemo reading a file name from the keyboard closing the file using the file name read from the keyboard reading data from the file

© 2000 Scott S Albert Common methods to test for the end of an input file A common programming situation is to read data from an input file but not know how much data the file contains In these situations you need to check for the end of the file There are two common ways to test for the end of a file: 1. Put a sentinel value at the end of the file and test for it. 2. Test for a special character that signals the end of the file (text files often have such a character).

© 2000 Scott S Albert Summary Text files contain strings of printable characters; they look intelligible to humans when opened in a text editor. Binary files contain numbers or data in non-printable codes; they look unintelligible to humans when opened in a text editor. C++ can process both binary and text files Always check for the end of the file when reading from a file. The way you check for end-of-file depends on the method you use to read from the file. A file name can be read from the keyboard into a String variable and the variable used in place of a file name. The class fstream has methods to test if a file exists and if it is read- and/or write-enabled.