Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.

Slides:



Advertisements
Similar presentations
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Advertisements

Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
I/O Streams as an Introduction to Objects and Classes
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
Chapter 15.
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.
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.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting 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 &
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.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
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.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
File I/O Version 1.0. Topics I/O Streams File I/O Formatting Text Files Handling Stream Errors File Pointers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Learners Support Publications Working with Files.
Lecture 14 Arguments, Classes and Files. Arguments.
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.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
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?
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
What is wrong in the following function definition
ifstreams and ofstreams
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Data File Handling in C++
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
File I/O.
Lecture 5A File processing Richard Gesick.
File I/O.
Basic File I/O and Stream Objects
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
File I/O in C++ I.
ifstreams and ofstreams
File I/O in C++ II.
File I/O in C++ I.
Presentation transcript:

Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi

In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based files – Random file access – File streams as function arguments Objectives (c) Asma AlOsaimi2

Working with files Binary files: store data in the same format that a computer has in main memory Text files : store data in which numeric values have been converted into strings of ASCII characters Files are opened in text mode (as text files) by default There are three basic operations that you will need to perform when working with disk files: – Open the file for input or output. – Process the file, by reading from or writing to the file. – Close the file. 3 (c) Asma AlOsaimi

Files and Streams C++ views each files as a sequential stream of bytes Operating system provides mechanism to determine end of file – End-of-file marker  EOF() – Count of total bytes in file When file opened – Object created, stream associated with it – File stream: A one-way transmission path used to connect a file stored on a physical device to a program 4(c) Asma AlOsaimi

 ios is the base class.  istream and ostream inherit from ios  ifstream inherits from istream (and ios)  ofstream inherits from ostream (and ios)  iostream inherits from istream and ostream (& ios)  fstream inherits from ifstream, iostream, and ofstream Classes for Stream I/O in C++ 5(c) Asma AlOsaimi

Iostream & fstream interactive (iostream) –cin - input stream associated with keyboard. –cout - output stream associated with display. –Cerr - standard output for error messages and associated with display. file stream  fstream – ifstream - File stream that receives or reads data from a file to a program. –ofstream - File stream that sends or writes data to a file. 6(c) Asma AlOsaimi

For each file your program uses, regardless of file’s type, a distinct file stream object must be created File Stream Objects (continued) (c) Asma AlOsaimi7

General File I/O Steps 1.Include the header file fstream in the program. 2.Declare file stream variables. 3.Connecting stream object name to external filename: opening a file 4.Determining whether successful connection has been made 5.Use the file stream variables with >>, <<, or other input/output functions (ex:read, write). 6.Closing connection: closing a file 8(c) Asma AlOsaimi

General File I/O Steps (cont.) 1. Include the header file fstream in the program. #include 2.Declare file stream variables. ifstream inStream; //input : read ofstream outStream; //Output :write Fstream both; //Input+output 3.Connecting stream object name to external filename: opening a file inStream.open("infile.dat"); outStream.open("outfile.dat"); 4.Determining whether successful connection has been made if (inStream.fail()) { cout << "Input file open failed\n"; exit(1);// requires } (c) Asma AlOsaimi9

General File I/O Steps (cont.) 5: Use the file stream variables with >>, <<, or other input/output functions. Read: int num; inStream >> num; //[Compare: cin >> num;] Write: outStream << num; //[Compare: cout << num;] NOTE: Streams are sequential – data is read and written in order – generally can't back up. 6: Closing connection: closing a file inStream.close(); outStream.close(); (c) Asma AlOsaimi10

Step#3 opening a file – Constructors take file name and file-open mode ofstream outStream ( "filename", fileOpenMode ); – To attach a file later ofstream outStream; outStream.open( "filename", fileOpenMode); – ofstream opened for output by default ofstream outStream( "student.dat", ios::out ); ofstream outStream( "student.dat"); 1)File name: " c:\data\student.dat" tells compiler exactly where to look 2)File name: " student.dat" this must be in the same directory as the program executable, or in the compiler's default directory 11(c) Asma AlOsaimi

Step#3 opening a file :File Open Mode NameDescription ios::inOpen file to read ios::outOpen file to write ios::appAll the data you write, is put at the end of the file. It calls ios::out ios::ateAll the data you write, is put at the end of the file. It does not call ios::out ios::truncDeletes all previous content in the file. (empties the file) ios::binaryOpens the file in binary mode. If you want to set more than one open mode, just use the OR operator- |. This way: ios::ate | ios::binary 12 (c) Asma AlOsaimi

Step#3 opening a file (cont.) In the case of an input file: – The file must exist before the open statement executes. – If the file does not exist, the open statement fails and the input stream enters the fail state In the case of an output file: – An output file does not have to exist before it is opened; – If the output file does not exist, the computer prepares an empty file for output. – If the designated output file already exists, by default, the old contents are erased when the file is opened. 13(c) Asma AlOsaimi

Step#4 successful connection ? Method #1 :By checking the stream variable; If ( ! both) { Cout << “Cannot open file”; } Method #2 : By using bool fail() function if ( both.fail() ) { cout } Method #3 : By using bool is_open() function. If ( !both.is_open()) { Cout << “File is not open.”; } 14(c) Asma AlOsaimi

Sequential access vs. Random access Sequential access: – start at beginning of file and go through data in file, in order, to end – to access 100 th entry in file, go through 99 preceding entries first Random access: – access data in a file in any order – can access 100 th entry directly (c) Asma AlOsaimi15

Sequential access (c) Asma AlOsaimi16

(c) Asma AlOsaimi Sequential access 17

(c) Asma AlOsaimi Sequential access 18

(c) Asma AlOsaimi //need #include int i =40; double j=10.00; cout<< setw(4) << i << setw(6) << j; //_ _40 _10.00 Sequential access while(inClientFile>> myVariable) Stops when EOF found (gets value 0 ) 19

(c) Asma AlOsaimi Sequential access 20