I. file input/output (DD 14.3-11, HR A-93-95, A-101-111) A. overview ------------------------------------------ FILE PROCESSING IN C++ (DD 14.3-11, HR A-93-95, A-101-111) Topics: Files and Streams Sequential Access files Random Access files Motivating problem: power failure at the hotel Essentials: #include #include ofstream outFile("output.txt"); ifstream inFile("input.txt"); int i; inFile >> i; outFile << i; outFile.write((char*)&i, sizeof(i)); inFile.read((char*)&i, sizeof(i)); ------------------------------------------ B. files and streams (DD 14.3) ------------------------------------------ FILE STREAMS def: a *stream* is a sequence of characters, indefinitely long Stream n bytes long: 0 1 2 3 4 5 6 7 n-1 [ | | | | | | | | ... | | | def: a *file* is a permenent collection of data. def: an *input file stream* is def: an *output file stream* is File states: nonexistent not in use | / ^ | open / | close v v | opened --> reading/writing ------------------------------------------ How many characters are in cin? 1. input from files (DD 14.5) ------------------------------------------ // countChars.C #include #include #include main() { ifstream inFile("test.txt"); if (!inFile) { cerr << "Cannot open " << "test.txt" << endl; return 1; } char ch; int count = 0; while (inFile >> ch) { count++; } cout << "There are " << count << " non-whitespace chars in " << "test.txt" << endl; return 0; } ------------------------------------------ 2. subtyping (DD p. 655) ------------------------------------------ SUBTYPING AMONG I/O TYPES ios istream ostream ifstream ofstream Example: extern istream& operator >>(istream& in, char & ch); ifstream inFile("my_data.txt"); inFile >> ch; def: type S is a *subtype* of T iff objects of type S can be used ------------------------------------------ 3. output to files (DD 14.4)) ------------------------------------------ PROBLEM Write a program that reads guest info from the terminal and stores it in a file guestDB.txt // Recovery.C #include #include #include #include #include "GuestInfo.h" #include "IODetails.h" main() { cout << "Guest Data recovery" << endl; const char dbname[] = "guestDB.txt"; return 0; } ------------------------------------------ C. sequential vs. random access files (DD 14.5-10) ------------------------------------------ SEQUENTIAL VS. RANDOM ACCESS FILES (DD 14.5-10) def: in *sequential access*, it takes linear time to access each record. def: in *random access* it takes constant time to access each record. ------------------------------------------ 1. operations (DD p. 660) ------------------------------------------ RANDOM ACCESS PRIMITIVES (DD p. 660) File as a sequence of bytes: 0 1 2 3 4 5 6 7 n-1 [ | | | | | | | | ... | | | file position pointer: [ ] To start reading from beginning: To skip to byte n: To move forward n bytes: To get the curent position: ------------------------------------------ 2. example of random access files (compare DD 14.8) ------------------------------------------ FIXED SIZE RECORDS // GuestInfoFixed.h #ifndef _GuestInfoFixed_h #define _GuestInfoFixed_h 1 const int STRSIZE = 100; struct GuestInfoFixed { char name[STRSIZE+1]; char address[STRSIZE+1]; int roomNumber; float charges; }; #endif ------------------------------------------ ------------------------------------------ // GuestFileCreation.C #include #include #include #include #include "GuestInfoFixed.h" #include "IODetails.h" main() { cout << "Guest File Creation" << endl; const char dbname[] = "guestDB.txt"; ofstream outFile(dbname, ios::out | ios::noreplace); if (!outFile) { cerr << "Cannot open " << dbname << ": " << strerror(errno) << endl; return 1; } GuestInfoFixed blankGuest = {"", "", 0, 0.0}; const int MAX_ROOM_NUM = 99; int i; for (i = 0; i <= MAX_ROOM_NUM; i++) { outFile.write((char*)&blankGuest, sizeof(GuestInfoFixed)); } return 0; } ------------------------------------------