CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction and File Processing Lecture 13.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - File Processing Outline 14.1 Introduction 14.2 The Data Hierarchy 14.3 Files and Streams.
 2006 Pearson Education, Inc. All rights reserved File Processing.
計算機概論實習 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.
FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace.
1 Lecture Note 10 – Templates and File Processing Outline Templates Function Templates Class Templates File Processing Sequential-Access File Random-Access.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
 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.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 File Processing Dr. Serdar ÇELEBİ. 2 Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
 2008 Pearson Education, Inc. All rights reserved File Processing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - File Processing Outline 14.1 Introduction 14.2 The Data Hierarchy 14.3 Files and Streams.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
計算機程式語言 Lecture 17-1 國立台灣大學生物機電系 林達德 17 File Processing.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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.
Student Book Input / Output in C++
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
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
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.
1 CSC241: Object Oriented Programming Lecture No 32.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
File Processing Part 2. Random Access File In sequential access file, record in a file created with the formatted output function fprintf are not necessarily.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
1 Today’s Objectives  Announcements Homework #5 is due next Monday, July 24. Since this date is so close to the end of the semester, no late assignments.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
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)
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management(cont.) Binary and random files.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 11 – File Processing
File Processing.
File Processing.
Programming with ANSI C ++
File Processing Part 2.
17 File Processing.
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
17 File Processing.
Chapter 14 – File Processing
Chapter 11 – File Processing
17 File Processing.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
Topics Input and Output Streams More Detailed Error Testing
C++ Programming Lecture 8 File Processing
EPSII 59:006 Spring 2004.
Presentation transcript:

CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.

CPSC 231 D.H. C++ File Processing 2 C++ Files and Streams §C++ views each files as a sequence of bytes. §Each file ends with an end-of-file marker. §When a file is opened, an object is created and a stream is associated with the object. §To perform file processing in C++, the header files and must be included. § includes and

CPSC 231 D.H. C++ File Processing 3 Creating a sequential file // Fig. 14.4: fig14_04.cpp D&D p.708 // Create a sequential file #include int main() { // ofstream constructor opens file ofstream outClientFile( "clients.dat", ios::out ); if ( !outClientFile ) { // overloaded ! operator cerr << "File could not be opened" << endl; exit( 1 ); // prototype in stdlib.h }

CPSC 231 D.H. C++ File Processing 4 Sequential file cout << "Enter the account, name, and balance.\n" << "Enter end-of-file to end input.\n? "; int account; char name[ 30 ]; float balance; while ( cin >> account >> name >> balance ) { outClientFile << account << ' ' << name << ' ' << balance << '\n'; cout << "? "; } return 0; // ofstream destructor closes file }

CPSC 231 D.H. C++ File Processing 5 Question. §What does the above program do?

CPSC 231 D.H. C++ File Processing 6 How to open a file in C++ ? Ofstream outClientFile(“clients.dat”, ios:out) OR Ofstream outClientFile; outClientFile.open(“clients.dat”, ios:out)

CPSC 231 D.H. C++ File Processing 7 File Open Modes ios:: app - (append) write all output to the end of file ios:: ate - data can be written anywhere in the file ios:: binary - read/write data in binary format ios:: in - (input) open a file for input ios::out - (output) open afile for output ios: trunc -(truncate) discard the files’ contents if it exists

CPSC 231 D.H. C++ File Processing 8 File Open Modes cont. ios:nocreate - if the file does NOT exists, the open operation fails ios:noreplace - if the file exists, the open operation fails

CPSC 231 D.H. C++ File Processing 9 How to close a file in C++? The file is closed implicitly when a destructor for the corresponding object is called OR by using member function close: outClientFile.close();

CPSC 231 D.H. C++ File Processing 10 Reading and printing a sequential file // Reading and printing a sequential file #include void outputLine( int, const char *, double ); int main() { // ifstream constructor opens the file ifstream inClientFile( "clients.dat", ios::in ); if ( !inClientFile ) { cerr << "File could not be opened\n"; exit( 1 ); }

CPSC 231 D.H. C++ File Processing 11 int account; char name[ 30 ]; double balance; cout << setiosflags( ios::left ) << setw( 10 ) << "Account" << setw( 13 ) << "Name" << "Balance\n"; while ( inClientFile >> account >> name >> balance ) outputLine( account, name, balance ); return 0; // ifstream destructor closes the file } void outputLine( int acct, const char *name, double bal ) { cout << setiosflags( ios::left ) << setw( 10 ) << acct << setw( 13 ) << name << setw( 7 ) << setprecision( 2 ) << resetiosflags( ios::left ) << setiosflags( ios::fixed | ios::showpoint ) << bal << '\n'; }

CPSC 231 D.H. C++ File Processing 12 File position pointer and classes provide member functions for repositioning the file pointer (the byte number of the next byte in the file to be read or to be written.) These member functions are: seekg (seek get) for istream class seekp (seek put) for ostream class

CPSC 231 D.H. C++ File Processing 13 Examples of moving a file pointer inClientFile.seekg(0) - repositions the file get pointer to the beginning of the file inClientFile.seekg(n, ios:beg) - repositions the file get pointer to the n-th byte of the file inClientFile.seekg(m, ios:end) -repositions the file get pointer to the m-th byte from the end of file nClientFile.seekg(0, ios:end) - repositions the file get pointer to the end of the file The same operations can be performed with function member seekp.

CPSC 231 D.H. C++ File Processing 14 Member functions tellg() and tellp(). Member functions tellg and tellp are provided to return the current locations of the get and put pointers, respectively. long location = inClientFile.tellg(); To move the pointer relative to the current location use ios:cur inClientFile.seekg(n, ios:cur) - moves the file get pointer n bytes forward.

CPSC 231 D.H. C++ File Processing 15 Updating a sequential file Data that is formatted and written to a sequential file cannot be modified easily without the risk of destroying other data in the file. If we want to modify a record of data, the new data may be longer than the old one and it could overwrite parts of the record following it.

CPSC 231 D.H. C++ File Processing 16 Problems with sequential files Sequential files are inappropriate for so- called “instant access” applications in which a particular record of information must be located immediately. These applications include banking systems, point-of-sale systems, airline reservation systems, (or any data-base system.)

CPSC 231 D.H. C++ File Processing 17 Random access files Instant access is possible with random access files. Individual records of a random access file can be accessed directly (and quickly) without searching many other records.

CPSC 231 D.H. C++ File Processing 18 Example of a Program that Creates a Random Access File // Fig : clntdata.h // Definition of struct clientData used in // Figs , 14.12, and #ifndef CLNTDATA_H #define CLNTDATA_H struct clientData { int accountNumber; char lastName[ 15 ]; char firstName[ 10 ]; float balance; }; #endif

CPSC 231 D.H. C++ File Processing 19 Creating a random access file // Creating a randomly accessed file sequentially #include #include "clntdata.h" int main() { ofstream outCredit( "credit1.dat", ios::out); if ( !outCredit ) { cerr << "File could not be opened." << endl; exit( 1 ); }

CPSC 231 D.H. C++ File Processing 20 clientData blankClient = { 0, "", "", 0.0 }; for ( int i = 0; i ( &blankClient ), sizeof( clientData ) ); return 0; }

CPSC 231 D.H. C++ File Processing 21 memebr function write The member function write outputs a fixed number of bytes beginning at a specific location in memory to the specific stream. When the stream is associated with a file, the data is written beginning at the location in the file specified by the “put” file pointer.

CPSC 231 D.H. C++ File Processing 22 The write function expects a first argument of type const char *, hence we used the reinterpret_cast to convert the address of the blankClient to a const char *. The second argument of write is an integer of type size_t specifying the number of bytes to written. Thus the sizeof( clientData ).

CPSC 231 D.H. C++ File Processing 23 Writing data randomly to a random file #include #include "clntdata.h" int main() { ofstream outCredit( "credit.dat", ios::ate ); if ( !outCredit ) { cerr << "File could not be opened." << endl; exit( 1 ); }

CPSC 231 D.H. C++ File Processing 24 cout > client.accountNumber; while ( client.accountNumber > 0 && client.accountNumber > client.lastName >> client.firstName >> client.balance;

CPSC 231 D.H. C++ File Processing 25 outCredit.seekp( ( client.accountNumber - 1 ) * sizeof( clientData ) ); outCredit.write( reinterpret_cast ( &client ), sizeof( clientData ) ); cout > client.accountNumber; } return 0; }

CPSC 231 D.H. C++ File Processing 26 Reading data from a random file #include #include "clntdata.h" void outputLine( ostream&, const clientData & ); int main() { ifstream inCredit( "credit.dat", ios::in ); if ( !inCredit ) { cerr << "File could not be opened." << endl; exit( 1 ); }

CPSC 231 D.H. C++ File Processing 27 cout ( &client ), sizeof( clientData ) );

CPSC 231 D.H. C++ File Processing 28 while ( inCredit && !inCredit.eof() ) { if ( client.accountNumber != 0 ) outputLine( cout, client ); inCredit.read( reinterpret_cast ( &client ), sizeof( clientData ) ); } return 0; }

CPSC 231 D.H. C++ File Processing 29 void outputLine( ostream &output, const clientData &c ) { output << setiosflags( ios::left ) << setw( 10 ) << c.accountNumber << setw( 16 ) << c.lastName << setw( 11 ) << c.firstName << setw( 10 ) << setprecision( 2 ) << resetiosflags( ios::left ) << setiosflags( ios::fixed | ios::showpoint ) << c.balance << '\n'; }

CPSC 231 D.H. C++ File Processing 30 The function read inCredit.read (reinterpret_cast (&client), sizeof(clientData)); The function inputs a specified (by sizeof(clientData)) number of bytes from the current position of the specified stream into an object.