Chapter 12: Advanced File Operations.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Advertisements

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.
 2006 Pearson Education, Inc. All rights reserved File Processing.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
 2006 Pearson Education, Inc. All rights reserved File Processing.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Chapter 15.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 13: Advanced File.
CSE Lectures 22 – Huffman codes
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Chapter 8 Data File Basics.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
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.
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
“After a day spent staring at a computer monitor, think of a book as a kind of screen saver for your brain” “One good reason why computers can do more.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
C++ FILE I/O.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
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 CSC241: Object Oriented Programming Lecture No 32.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
Learners Support Publications Working with Files.
Lecture 14 Arguments, Classes and Files. Arguments.
FILE HANDLING(WORKING WITH FILES) FILE- A file is a collection of related data stored in a particular area on the disk. STREAMS- interface between the.
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)
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.
Starting Out with C++ 1 Chapter 12: File Operations What is a File? A file is a collection on information, usually stored on some electronic medium. Information.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
Chapter 13: Advanced File and I/O Operations
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.
FILE HANDLING IN C++.
17 File Processing.
Lecture 5A File processing Richard Gesick.
Working with Data Files
17 File Processing.
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
C++ FileIO pepper.
Topics Input and Output Streams More Detailed Error Testing
Chapter 3 Input output.
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
C++ Programming Lecture 8 File Processing
Presentation transcript:

Chapter 12: Advanced File Operations

12.1 File Operations

File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to files Used in many applications: Word processing Databases Spreadsheets Compilers

Using Files Requires fstream header file use ifstream data type for input files use ofstream data type for output files use fstream data type for both input, output files Can use >>, << to read from, write to a file Can use eof member function to test for end of input file

fstream Object fstream object can be used for either input or output Must specify mode on the open statement Sample modes: ios::in – input ios::out – output Can be combined on open call: dFile.open("class.txt", ios::in | ios::out);

File Access Flags

Using Files - Example // copy 10 numbers between files // open the files fstream infile("input.txt", ios::in); fstream outfile("output.txt", ios::out); int num; for (int i = 1; i <= 10; i++) { infile >> num; // use the files outfile << num; } infile.close(); // close the files outfile.close();

Default File Open Modes ifstream: open for input only file cannot be written to open fails if file does not exist ofstream: open for output only file cannot be read from file created if no file exists file contents erased if file exists

More File Open Details Can use filename, flags in definition: ifstream gradeList("grades.txt"); File stream object set to 0 (false) if open failed: if (!gradeList) ... Can also check fail member function to detect file open error: if (gradeList.fail()) ...

File Output Formatting 12.2 File Output Formatting

File Output Formatting Use the same techniques with file stream objects as with cout: showpoint, setw(x), showprecision(x), etc. Requires iomanip to use manipulators

Program 12-3 (Continued)

Passing File Stream Objects to Functions 12.3 Passing File Stream Objects to Functions

Passing File Stream Objects to Functions It is very useful to pass file stream objects to functions Be sure to always pass file stream objects by reference

More Detailed Error Testing 12.4 More Detailed Error Testing

More Detailed Error Testing Can examine error state bits to determine stream status Bits tested/cleared by stream member functions ios::eofbit set when end of file detected ios::failbit set when operation failed ios::hardfail set when error occurred and no recovery ios::badbit set when invalid operation attempted ios::goodbit set when no other bits are set

Member Functions / Flags eof() true if eofbit set, false otherwise fail() true if failbit or hardfail set, false otherwise bad() true if badbit set, false otherwise good() true if goodbit set, false otherwise clear() clear all flags (no arguments), or clear a specific flag

From Program 12-6

Member Functions for Reading and Writing Files 12.5 Member Functions for Reading and Writing Files

Member Functions for Reading and Writing Files Functions that may be used for input with whitespace, to perform single character I/O, or to return to the beginning of an input file Member functions: getline: reads input including whitespace get: reads a single character put: writes a single character

The getline Function Three arguments: Name of a file stream object Name of a string object Delimiter character of your choice Examples, using the file stream object myFile, and the string objects name and address: getline(myFile, name); getline(myFile, address, '\t'); If left out, '\n' is default for third argument

Single Character I/O get: read a single character from a file char letterGrade; gradeFile.get(letterGrade); Will read any character, including whitespace put: write a single character to a file reportFile.put(letterGrade);

Working with Multiple Files 12.6 Working with Multiple Files

Working with Multiple Files Can have more than file open at a time in a program Files may be open for input or output Need to define file stream object for each file

12.7 Binary Files

Binary Files Binary file contains unformatted, non-ASCII data Indicate by using binary flag on open: inFile.open("nums.dat", ios::in | ios::binary);

Binary Files Use read and write instead of <<, >> char ch; // read in a letter from file inFile.read(&ch, sizeof(ch)); // send a character to a file outFile.write(&ch, sizeof(ch)); address of where to put the data being read in. The read function expects to read chars how many bytes to read from the file

Binary Files To read, write non-character data, must use a typecast operator to treat the address of the data as a character address int num; // read in a binary number from a file inFile.read(reinterpret_cast<char *>&num, sizeof(num)); // send a binary value to a file outf.write(reinterpret_cast<char *>&num, treat the address of num as the address of a char

Creating Records with Structures 12.8 Creating Records with Structures

Creating Records with Structures Can write structures to, read structures from files To work with structures and files, use ios::binary file flag upon open use read, write member functions

Creating Records with Structures struct TestScore { int studentId; double score; char grade; }; TestScore oneTest; ... // write out oneTest to a file gradeFile.write(reinterpret_cast<char *> (&oneTest), sizeof(oneTest));

12.9 Random-Access Files

Random-Access Files Sequential access: start at beginning of file and go through data in file, in order, to end to access 100th entry in file, go through 99 preceding entries first Random access: access data in a file in any order can access 100th entry directly

Random Access Member Functions seekg (seek get): used with files open for input seekp (seek put): used with files open for output Used to go to a specific position in a file

Random Access Member Functions seekg,seekp arguments: offset: number of bytes, as a long mode flag: starting point to compute offset Examples: inData.seekg(25L, ios::beg); // set read position at 26th byte // from beginning of file outData.seekp(-10L, ios::cur); // set write position 10 bytes // before current position

Important Note on Random Access If eof is true, it must be cleared before seekg or seekp: gradeFile.clear(); gradeFile.seekg(0L, ios::beg); // go to the beginning of the file

Random Access Information tellg member function: return current byte position in input file long int whereAmI; whereAmI = inData.tellg(); tellp member function: return current byte position in output file whereAmI = outData.tellp();

Opening a File for Both Input and Output 12.10 Opening a File for Both Input and Output

Opening a File for Both Input and Output File can be open for input and output simultaneously Supports updating a file: read data from file into memory update data write data back to file Use fstream for file object definition: fstream gradeList("grades.dat", ios::in | ios::out); Can also use ios::binary flag for binary data