نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management(cont.) Binary and random files.

Slides:



Advertisements
Similar presentations
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Advertisements

1 Classes and Data Abstraction and File Processing Lecture 13.
 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.
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.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
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.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
1 Structure part 1 & File Processing. 2 Structures.
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.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
 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.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
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.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Exception Handling, File Processing Lecture 11 March 30, 2004.
Learners Support Publications Working with Files.
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.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
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.
Chapter 11 – File Processing
File Processing.
File Processing.
File Processing Part 2.
17 File Processing.
Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
File Processing (Cont.) and Structures
CPSC 231 D.H. C++ File Processing
17 File Processing.
Chapter 14 – File Processing
Chapter 11 – File Processing
File I/O with Records Lesson xx
17 File Processing.
Topics Input and Output Streams More Detailed Error Testing
Chapter 3 Input output.
Pertemuan 14 File Processing II
Recitation Course 0603 Speaker: Liu Yu-Jiun.
EPSII 59:006 Spring 2004.
Presentation transcript:

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management(cont.) Binary and random files.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 2 Random-Access Files Instant access Want to locate record quickly Airline reservations, ATMs Sequential files must search through each one Random-access files are solution Instant access Insert record without destroying other data Update/delete items without changing other data

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 3 Random-Access Files C++ imposes no structure on files Programmer must create random-access files Simplest way: fixed-length records Calculate position in file from record size and key

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 4 Creating a Random-Access File " " ( char * ) vs ( int ) char * takes 8 bytes (1 for each character + null) int takes fixed number of bytes (perhaps 4) 123 same size in bytes as << operator and write() outFile << number Outputs number ( int ) as a char * Variable number of bytes outFile.write( const char *, size ); Outputs raw bytes Takes pointer to memory location, number of bytes to write Copies data directly from memory into file Does not convert to char *

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 5 Creating a Random-Access File Example outFile.write( reinterpret_cast (&number), sizeof( number ) ); &number is an int * Convert to const char * with reinterpret_cast sizeof(number) Size of number (an int ) in bytes read function similar (more later) Must use write / read between compatible machines Only when using raw, unformatted data Use ios::binary for raw writes/reads

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 6 Creating a Random-Access File Usually write entire struct or object to file Problem statement Credit processing program Store at most 100 fixed-length records Record Account number (key) First and last name Balance Account operations Update, create new, delete, list all accounts in a file Next: program to create blank 100-record file

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 7 clientData. h (1 of 2) 1 // Fig clientData.h 2 // Class ClientData definition used in Fig –Fig #ifndef CLIENTDATA_H 4 #define CLIENTDATA_H 5 #include 6 #include 7 using std::string; 9 class ClientData { 10 public: 11 // default ClientData constructor 12 ClientData( int = 0, string = "", string = "", double = 0.0 ); // accessor functions for accountNumber 15 void setAccountNumber( int ); 16 int getAccountNumber() const; 18 // accessor functions for lastName 19 void setLastName( string ); 20 string getLastName() const;

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 8 clientData. h (2 of 2) 21 // accessor functions for firstName 22 void setFirstName( string ); 23 string getFirstName() const; // accessor functions for balance 26 void setBalance( double ); 27 double getBalance() const; private: 30 int accountNumber; 31 char lastName[ 15 ]; 32 char firstName[ 10 ]; 33 double balance; 35 }; // end class ClientData #endif

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 9 Fig14.4.cpp (1 of 2) 1 // Fig. 14.4cpp 2 // Creating a randomly accessed file. 3 #include 4 using std::cerr; 5 using std::endl; 6 using std::ios; 8 #include 9 using std::ofstream; 11 #include 12 #include "clientData.h" // ClientData class definition 14 int main() { 15 ofstream outCredit( "credit.dat", ios::binary ); 17 // exit program if ofstream could not open file 18 if ( !outCredit ) { 19 cerr << "File could not be opened." << endl; 20 exit( 1 ); 21 } // end if

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 10 fig14_4.cp p (2 of 2) // create ClientData with no information 24 ClientData blankClient; // output 100 blank records to file 27 for ( int i = 0; i < 100; i++ ) 28 outCredit.write( 29 reinterpret_cast ( &blankClient ), 30 sizeof( ClientData ) ); return 0; } // end main

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 11 Writing Data Randomly to a Random- Access File Use seekp to write to exact location in file Where does the first record begin? Byte 0 The second record? Byte 0 + sizeof(object) Any record? (recordNum - 1) * sizeof(object)

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 12 fig14_5.cp p (2 of 4) 19 int main() { 20 int accountNumber; 21 char lastName[ 15 ]; 22 char firstName[ 10 ]; 23 double balance; 25 ofstream outCredit( "credit.dat", ios::binary ); 27 // exit program if ofstream cannot open file 28 if ( !outCredit ) { 29 cerr << "File could not be opened." << endl; 30 exit( 1 ); 31 } // end if 33 cout << "Enter account number " 34 << "(1 to 100, 0 to end input)\n? "; 36 // require user to specify account number 37 ClientData client; 38 cin >> accountNumber; 39 client.setAccountNumber( accountNumber );

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني // user enters information, which is copied into file 41 while ( client.getAccountNumber() > 0 && 42 client.getAccountNumber() <= 100 ) { 44 // user enters last name, first name and balance 45 cout << "Enter lastname, firstname, balance\n? "; 46 cin >> setw( 15 ) >> lastName; 47 cin >> setw( 10 ) >> firstName; 48 cin >> balance; // set record lastName, firstName and balance values 51 client.setLastName( lastName ); 52 client.setFirstName( firstName ); 53 client.setBalance( balance ); // seek position in file of user-specified record 56 outCredit.seekp( ( client.getAccountNumber() - 1 ) * sizeof( ClientData ) ); // write user-specified information in file 59 outCredit.write(reinterpret_cast ( &client ), sizeof( ClientData ) ); fig14_5.cpp (3 of 4)

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 14 fig14_5.cp p (4 of 4) 60 // enable user to specify another account number 61 cout << "Enter account number\n? "; 62 cin >> accountNumber; 63 client.setAccountNumber( accountNumber ); } // end while return 0; } // end main

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 15 fig14_5.cpp output (1 of 1) Enter account number (1 to 100, 0 to end input) ? 37 Enter lastname, firstname, balance ? Barker Doug 0.00 Enter account number ? 29 Enter lastname, firstname, balance ? Brown Nancy Enter account number ? 96 Enter lastname, firstname, balance ? Stone Sam Enter account number ? 88 Enter lastname, firstname, balance ? Smith Dave Enter account number ? 33 Enter lastname, firstname, balance ? Dunn Stacey Enter account number ? 0 Notice that accounts can be created in any order.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 16 Reading Data Sequentially from a Random-Access File read - similar to write Reads raw bytes from file into memory inFile.read( reinterpret_cast ( &number ), sizeof( int ) ); &number : location to store data sizeof(int) : how many bytes to read Do not use inFile >> number with raw bytes >> expects char * Upcoming program Output data from a random-access file Go through each record sequentially If no data (accountNumber == 0) then skip

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 17 fig14_6.cpp (2 of 3) 26 int main() { 27 ifstream inCredit( "credit.dat", ios::in ); // exit program if ifstream cannot open file 30 if ( !inCredit ) { 31 cerr << "File could not be opened." << endl; 32 exit( 1 ); 33 } // end if 35 cout << left << setw( 10 ) << "Account" << setw( 16 ) 36 << "Last Name" << setw( 11 ) << "First Name" << left 37 << setw( 10 ) << right << "Balance" << endl; 39 ClientData client; // create record // read first record from file 42 inCredit.read( reinterpret_cast ( &client ), 43 sizeof( ClientData ) ); 44

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني // read all records from file 46 while ( inCredit && !inCredit.eof() ) { 47 // display record 48 if ( client.getAccountNumber() != 0 ) 49 outputLine( cout, client ); 51 // read next from file 52 inCredit.read( reinterpret_cast ( &client ), 53 sizeof( ClientData ) ); 54 } // end while 56 return 0; 57 } // end main 59 // display single record 60 void outputLine( ostream &output, const ClientData &record ) { 61 output << left << setw( 10 ) << record.getAccountNumber() 62 << setw( 16 ) << record.getLastName().data() 63 << setw( 11 ) << record.getFirstName().data() 64 << setw( 10 ) << setprecision( 2 ) << right << fixed 65 << showpoint << record.getBalance() << endl; 66 } // end outputLine fig14_6.cpp (3 of 3)

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 19 fig14_6.cpp output : Account Last Name First Name Balance 29 Brown Nancy Dunn Stacey Barker Doug Smith Dave Stone Sam 34.98