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.

Slides:



Advertisements
Similar presentations
Chapter 10 Input/Output Streams
Advertisements

Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
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.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 7: Files Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 13 Advanced.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
Chapter 5: Loops and Files.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
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.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 13: Advanced 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.
Program Input and the Software Design Process ROBERT REAVES.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Chapter 8 Data File Basics.
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Loops and Files. 5.1 The Increment and Decrement Operators.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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?
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
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.
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.
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.
Copyright © 2003 Pearson Education, Inc.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
File I/O.
Lecture 5A File processing Richard Gesick.
Interactive I/O Input from keyboard Must prompt user User friendly
Basic File I/O and Stream Objects
when need to keep permanently
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Standard Input/Output Stream
File I/O.
File I/O in C++ I.
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Lecture 9 Files Handling
File I/O in C++ I.
Presentation transcript:

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 Binary Files

File Basics  A file—set of data stored permanently on an external storage device—e.g., hard disk, USB drive, etc.  Programs can read from and write to files.  File types  Binary files / Text files  Sequential / Random files  Program files / Data files  Image files / Audio files / Video files  Application files (Word, Excel) / Application Data files (.doc,.docx,.xls, xlsx) 13-3

Basic Steps in Using a File 1.Open the file 2.Use (read from, write to) the file 3.Close the file 13-4

File Stream Objects  Use of files requires file stream objects  There are three types of file stream objects (1) ifstream objects: used for input (2) ofstream objects: used for output (3) fstream objects: used for both input and output 13-5 Computer MemoryExternal Storage A B C D E A B C D E F G H I J K L M N O P Q R S T Ifstream infile myfile.dat

Opening a File for Input  Create an ifstream object in your program ifstream inFile;  Open the file by passing its name to the stream’s open member function inFile.open("myfile.dat"); 13-6

Getting File Name from User  Define file stream object, variable to hold file name ifstream inFile; char fileName[81]; // C-string  Prompt user to enter filename and read the filename cout << "Enter filename: "; cin.getline(fileName, 81);  Open the file inFile.open(fileName); 13-7

Using C++ string instead of C-string  Define file stream object, variable to hold file name ifstream inFile; string fileName;  Prompt user to enter filename and read the filename cout << "Enter filename: "; getline(cin, fileName);  Open the file inFile.open(fileName.c_str()); // Note: string to C-string conversion 13-8

Opening a File for Output  Create an ofstream object in your program ofstream outFile;  Open the file by passing its name to the stream’s open member function outFile.open("myfile.dat"); 13-9

Detecting File Open Errors Two methods for detecting if a file open failes (1) Call fail() on the stream inFile.open("myfile"); if (inFile.fail()) { cout << "Can't open file"; exit(1); } 13-10

Detecting File Open Errors (2) Test the status of the stream using the ! operator inFile.open("myfile"); if (!inFile) { cout << "Can't open file"; exit(1); } 13-11

Using fail() to detect eof Example of reading all integers in a file //attempt a read int x; infile >> x; while (!infile.fail()) { //success, so not eof cout << x; //read again infile >> x; } 13-12

Your Turn  Write a program which allows the user to input 3 lines of text. The program writes each line to a file. It then reads those text lines from the file and echo prints them to the screen.

Passing File Stream Objects to Functions  File stream objects keep track of current read or write position in the file  Always use pass by reference when passing file stream object to function 13-14

Passing File Stream Objects to Functions //print file contents to screen void printFile(ifstream &fin); int main(){ ifstream fin; fin.open(“myfile.dat”); printFile(fin);... } 13-15

Passing File Stream Objects to Functions // print file contents to screen void printFile(ifstream &fin){ int x; fin >> x; while(!in.fail()){ cout << x << " "; fin >> x; } } 13-16

Passing File Stream Objects //Write to file void writeTo(ofstream &fout, int list[], int count); int main(){ int list[] = {2, 4, 6, 8, 10}; int count = 5; ofstream fout; fout.open(“myfile.dat”); writeTo(fout, list, count);... } 13-17

Passing File Stream Objects // Write to file void writeTo(ofstream &fout, int list[], int count); for (int i = 0; i < count; i++){ fout << list[i] << ‘ ‘; } } 13-18

Your Turn  Convert the preceding “Your Turn” program so that writing data to a file and reading data from the file use two user-defined functions.

Binary File  Binary Files contain numeric data without conversion to ASCII characters.  Storing 1234: in Text File: 1 in Binary File: ‘1’ ‘2’‘3’‘4’