16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and.

Slides:



Advertisements
Similar presentations
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
Advertisements

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
File streams Chapter , ,
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 11: Handling Real-World Data Entry.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Guide To UNIX Using Linux Third Edition
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.
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.
Input and Output Chapter 3. 2 Chapter Topics  I/O Streams, Devices  Predefined Functions  Input Failure  Formatting Output  Formatting Tools  File.
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.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
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
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Fundamental Programming Command Line Parameters and File I/O.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
C++ Streams Lecture-2.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
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.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
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:
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
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.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
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.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Learners Support Publications Working with Files.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
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.
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
C++ Basic Input and Output (I/O)
Copyright © 2003 Pearson Education, Inc.
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
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.
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
Managing console i/o operations
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 2 part #3 C++ Input / Output
Dale/Weems/Headington Program Input and the Software Design Process
Presentation transcript:

16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and displayed to the screen. Limiting: Sometimes there is much data to be processed. Sometimes there is much data to be processed. Sometimes we want a permanent record of the data. Sometimes we want a permanent record of the data.

26/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Files One type of file is a data file: a file which is created/updated and/or input by a program. Two Types: Random Access: the program can input any record from the file by specifying a record number. We will not be studying these – they are somewhat complicated. Random Access: the program can input any record from the file by specifying a record number. We will not be studying these – they are somewhat complicated. Sequential: the program may only write or read (not both) adjacent data from the beginning of the file. Sequential: the program may only write or read (not both) adjacent data from the beginning of the file.

36/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Sequential Disk Files AKA text files or ASCII files (and other names). They can be created with many different software packages. Notepad will create text files. Disk files have: A directory. A directory. A file name. A file name.

46/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Streams A sequential disk file is treated as a stream. A stream: Is a sequence of characters. Is a sequence of characters. Has whitespace embedded in it. Has whitespace embedded in it. Blanks, tabs, newlines. Whitespace is used to control input operations.

56/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Steps to use a data file #include to access library. Create an object of type ifstream or ofstream. A file will be used for input only or output only. A file will be used for input only or output only. This object is used to control I/O (file-control block). This object is used to control I/O (file-control block). Open the file. Do inputs or outputs to the file. Close the file.

66/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Opening/Closing Data Files.open( ); or.open( );.open( ); or.open( ); Append:.open(, ios::app); Examples: timecards.open(“week txt”); timecards.open(“week txt”); report.open(“todays_sales.txt”); report.open(“todays_sales.txt”); weights.open(exper_filename); weights.open(exper_filename); When all done doing I/O, close the file..close(); or.close();.close(); or.close();

76/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Reading Use the extractor operator (>>): weights >> w; weights >> w; employees >> name; employees >> name; Use the getline function: getline(timecards,emp_data); getline(timecards,emp_data); getline(weights,bridge_data,’\n’); getline(weights,bridge_data,’\n’); Use the get function for character-at-a-time input: inf.get(c); inf.get(c); Use the eof function to test whather eof has been reached – it is set when the last data item is read. while (!timecards.eof()) { … } while (!timecards.eof()) { … }

86/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Echoing Inputs Because the input is not typed on the screen, it is useful to the user if data file input is echoed to the screen or output file. This way the user knows what the input is and knows it was read correctly.

96/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Writing Use the insertion operator (<<): report << emp_line; report << emp_line; results << new_weight; results << new_weight; Use the put function for character at a time output: outf.put(c); outf.put(c);

106/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files I/O Manipulators Functions in the libraries iostream or iomanip. Control the format of output (or sometimes input). setw(int n): set the width of the next output value. setw(int n): set the width of the next output value. setprecision(int n): set the precision for output. setprecision(int n): set the precision for output. showpoint: causes a decimal point to always be displayed. showpoint: causes a decimal point to always be displayed. Left, right: cause output to be left or right justified in output field. Left, right: cause output to be left or right justified in output field. See table 8.3 on page 415.

116/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files I/O Errors.fail() or.fail() will return true if the last I/O operation failed..fail() or.fail() will return true if the last I/O operation failed. Why might an I/O operation fail? File does not exist for opening. File does not exist for opening. Reading past end-of-file. Reading past end-of-file. Writing to a file when the disk is full. Writing to a file when the disk is full. Others. Others. Typically follow any I/O operation with: if.fail() { }

126/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Cin/Cout The keyboard is just handled as a pre- defined and opened input stream called cin. The screen is just handled as a pre- defined and opened output stream called cout.

Files versus Humans When input is coming from the keyboard through cin, if there is an error in the input, the program can identify it and correct it by re-prompting the user (interactive). But when the input is coming from a file, there generally is not any method for correcting errors in input and continuing – all the program can do is report the error and terminate gracefully without doing any more processing. 136/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

146/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Reading Chapter 8 covers these same concepts. Reviewing them may help clarify the material.

Batch Processing What: a program that receives all of its input, then does all of its processing and output to permanent storage files (versus interactive programs). Why: Programs that process files may take a relatively long time to complete, depending on the size of the input, the amount of processing, and the output. 156/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

Batch Processing Main function command line arguments and return value: int main(int argc, char *argv[]) Enables a program to be executed from the command line with no user intervention cerr: similar to cout, but used for error messages exit(int error_code): immediately terminates the program, returning error_code EXIT_SUCCESS, EXIT_FAILURE: predefined constants that may be used to signal exit status. 166/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

Batch Processing I/O Redirection The input from the keyboard and the output to the screen may be redirected from the command line. The input from the keyboard and the output to the screen may be redirected from the command line. This allows a program to be executed in the background or its own window if it does use cin or cout. This allows a program to be executed in the background or its own window if it does use cin or cout. This also allows for piping: the output of one program becomes the input of another program. UNIX does this using filters: reusable tools. EG: sort, grep, awk, sed, more, head, tail. This also allows for piping: the output of one program becomes the input of another program. UNIX does this using filters: reusable tools. EG: sort, grep, awk, sed, more, head, tail. 176/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

Batch Processing This material on batch processing is useful for working in production environments or for working in a command line oriented operating system like UNIX. Data can be cut-and-pasted into or out of a console window, which works well for small amounts of data. 186/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files