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.

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.
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.
 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.
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.
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.
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
 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.
計算機程式語言 Lecture 17-1 國立台灣大學生物機電系 林達德 17 File Processing.
Fundamental File Processing Operations C++
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.
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
C++ FILE I/O.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Student Book Input / Output in C++
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
File I/O Version 1.0. Topics I/O Streams File I/O Formatting Text Files Handling Stream Errors File Pointers.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
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.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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++
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
File Processing.
File Processing.
Programming with ANSI C ++
17 File Processing.
Introduction to Programming
Data File Handling in C++
C ++ MULTIPLE CHOICE QUESTION
CISC/CMPE320 - Prof. McLeod
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
17 File Processing.
Chapter 14 – File Processing
File I/O with Records Lesson xx
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
CISC/CMPE320 - Prof. McLeod
C++ Programming: chapter 6 – iostream
C++ Programming Lecture 8 File Processing
File I/O in C++ II.
Input/Output Streams, Part 2
Presentation transcript:

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 these stream template specializations. A 9. File Processing 0001 Jane Gloria Doe accnt name balance 0023 Gloria record Gloria field G byte bit file Data Hierarchy data base 0 struct or class in C++ and must be included.

2008YeungNam Univ. SE Lab. #include using std::cout; using std::cin; using std::ios; #include using std::ofstream; struct Client { int a; // account char n[20]; // name double b; // balance }; int main() { ofstream osf("C:\\client.txt", ios::out); if ( !osf ) return 1; Client c; cout << "Enter account name balance:\n? "; while ( cin >> c.a >> c.n >> c.b ) { osf<<c.a<<' '<<c.n<<' '<<c.b<<'\n'; cout << "? "; } osf.close(); // optional return 0; } csf.cpp 2  C++ imposes no structure, such as “record”, on a file. Creating a Sequential File File I/O “ lpt1: ” for printer default path: Current project’s path Open mode ); Default open mode for ofstream 9. File Processing A ofstream osf; osf.open("C:\\client.txt"); Enter account name balance: ? 0001 Jane ? 0023 Gloria ? 0035 Doe 89.00^z ^z

2008YeungNam Univ. SE Lab. 3 File Open Modes File I/O opens a file for binary input or output ios::binary discards the file’s contents if it exist. This is the default action for ios::out ios::trunc opens a file for output ios::out opens a file for input ios::in opens a file for output and move to the end of the file. Data can be written anywhere in the file. ios::ate writes all output to the end of file. ios::app DescriptionMode 9. File Processing A

2008YeungNam Univ. SE Lab. 4 Reading a Sequential File File I/O void putClient( const Client& c ) { static int title = 1; if ( title && title-- ) cout << "Acnt...Name Balance\n" << setiosflags(ios::fixed|ios::showpoint); cout<< setw(4) << right << setfill('0') << c.a << ' ' << setw(13) << left << setfill(' ') << c.n << setw(7) << right << setprecision(2)<< c.b <<'\n'; } int main( ) { ifstream isf; isf.open( "C:\\client.txt", ios::in ); if ( !isf ) { cerr << "Open failed...\n"; return 1; } Client c; while ( isf >> c.a >> c.n >> c.b ) putClient( c ); return 0; } #include using std::cout; using std::cin; using std::cerr; using std::ios; #include using std::ifstream; #include using std::setiosflags; using std::setprecision; using std::setw; using std::setfill; using std::left; using std::right; struct Client { int a; char n[20]; double b; }; 9. File Processing A rsf.cpp Acnt...Name Balanc e 0001 Jane Gloria Doe 89.00

2008YeungNam Univ. SE Lab. #include using std::cout; using std::cin; using std::ios; #include using std::ofstream; struct Client { int a; char n[20]; double b; }; int main() { ofstream asf( "C:\\client.txt", ios::app ); if ( !asf ) return 1; Client c; cout << "Enter account name balance:\n? "; while ( cin >> c.a >> c.n >> c.b ) { asf << c.a << ' ' << c.n << ' ' << c.b << '\n'; cout << "? "; } return 0; } asf.cpp 5 Appending a Sequential File File I/O 9. File Processing A Enter account name balance: ? 0047 Jack ? 0051 Thomas ^z ^z

2008YeungNam Univ. SE Lab. 69. File Processing File Position Pointers File I/O istream is long cur_file_pos = is.tellg() is.seekg(5, ios::beg) is.seekg(3, ios::cur) “tell get” file position pointer is.seekg(4, ios::end) is.seekg(0) is.seekg(0, ios::end) default seek direction A  The byte numbers of the next byte in the file to be read or written  The same operations can be performed with ostream MFs tellp() and seekp() “seek put” file position pointer

2008YeungNam Univ. SE Lab. #include using std::cout; #include using std::ifstream; int main() { int x, n, s; // number, count, sum double m; // mean ifstream nf("c:\\numbers.txt"); if ( !nf ) return 1; n = s = 0; while ( nf >> x ) ++n, s += x; m = double(s) / n; nf.clear(); // reset EOFBIT nf.seekg(0); // move to beginning of file n = 0; while ( nf >> x ) if ( x > m ) ++n; cout << n << " number" << (n<2?"":"s") << " exceed" << (n<2?"s":"") << " the average " << m << '\n'; return 0; } avg.cpp 7  Count the numbers exceeding their average value. File I/O 9. File Processing A Reading a File Twice 12 numbers exceed the average 23.5

2008YeungNam Univ. SE Lab. 8 File I/O 9. File Processing A Updating Sequential Access Files  insert  delete

2008YeungNam Univ. SE Lab : : : : : 46 : 50 : : 99 File I/O 9. File Processing A Random-Access File  analogous to an array of records on a disc. Jane e+02Gloria e+02Doe e+02Jack e+02 Thomas e

2008YeungNam Univ. SE Lab. 10 File I/O 9. File Processing A Creating a Random-Access File cout << "Account[1.." << capacity << "] Name Balance:\n? "; while ( cin >> a >> c.n >> c.b ) { if ( a 100 ) continue; orf.seekp( (a-1)*sz ); orf.write( reinterpret_cast< const char*>(&c), sz ); cout << "? "; } orf.close(); return 0; } #include using std::cout; using std::cin; using std::ios; #include using std::ofstream; struct Client { char n[20]; double b; }; const int sz = sizeof( Client ); int main() { char file[ ] = "C:\\client.bin"; const int capacity = 100; ofstream orf( file, ios::binary ); if ( !orf ) return 1; int a; // account number Client c = { "", 0.0 }; for ( a = 0; a<capacity; ++a ) orf.write( reinterpret_cast< const char*>(&c), sz ); orf.close(); orf.open( file, ios::binary ); if ( !orf ) return 2; crf.cpp Account[1..100] Name Balance: ? 35 Doe ? 1 Jane ? 51 Thomas ? 23 Gloria ? 47 Jack 30.80^z ^z

2008YeungNam Univ. SE Lab. #include using std::cout; using std::ios; #include using std::ifstream; struct Client { char n[20]; double b; }; const int sz = sizeof( Client ); int main( ) { char file[ ] = "C:\\client.bin"; const int capacity = 100; ifstream irf( file, ios::binary ); if ( !irf ) return 1; int a; // account number Client c; for ( a=1; a<=capacity; ++a ) { irf.read( reinterpret_cast (&c), sz ); if ( !irf ) break; if ( c.n[0] != '\0' ) cout << a <<' '<<c.n<<' '<< c.b << '\n'; } irf.close(); return 0; } rrf.cpp 11 File I/O 9. File Processing A Reading a Random-Access File Sequentially fstream fstream irf( file, ios::binary|ios::in ); 1 Jane Gloria Doe Jack Thomas

2008YeungNam Univ. SE Lab. 12 File I/O 9. File Processing A Updating Random-Access File Randomly [i|d|r|p|e] Acc ? i 3 name balance? Anny ? r 23 Gloria => Gloria ? d 47 ? p 1 Jane ? p 3 Anny ? p 23 Gloria ? p ? e ^z ^z #include using namespace std; #define Rec( t ) reinterpret_cast (&t), sz struct Client { char n[20]; double b; }; const int sz = sizeof( Client ); int main() { char file[] = "C:\\client.bin"; const int capacity = 100; fstream urf( file, ios::in|ios::out ); if ( !urf ) return 1; Client c; Client blank = { "", 0.0 }; int acc; // account char ucc; // update control code long fpp; // file position pointer urf.cpp

2008YeungNam Univ. SE Lab. 13 File I/O cout << setiosflags( ios::fixed ) << setprecision( 2 ); cout << "[i|d|r|p|e] acc"; while ( cout > ucc ) { cin >> acc; // need to check fpp = (acc-1)*sz; switch ( tolower(ucc) ) { case 'i': // insert cout << "name balance? "; cin >> c.n >> c.b; urf.seekp( fpp ).write( Rec(c) ); break; case 'd': // delete urf.seekp( fpp ).write( Rec(blank) ); break; case 'r': // replace urf.seekg( fpp ).read( Rec(c) ); cout "; cin >> c.n >> c.b; urf.seekp( fpp ).write( Rec(c) ); break; case 'p': // print urf.seekg( fpp ).read( Rec(c) ); cout << c.n << " " << c.b << "\n"; break; 9. File Processing A case 'e': // end return 0; } return 0; } [i|d|r|p|e] Acc ? i 3 name balance? Anny ? r 23 Gloria => Gloria ? d 47 ? p 1 Jane ? p 3 Anny ? p 23 Gloria ? p ? e ^z

2008YeungNam Univ. SE Lab. 14 File I/O of Objects File I/O 9. File Processing #include using std::cin; using std::cout; using std::istream; using std::ostream; #include class Dog { public: Dog() : name(new char(0)), age(0) { } Dog(char* s, int a=0) : age(a) { name = strdup( s ); } ~Dog() { delete []name; } friend istream& operator >>( istream&, Dog& ); friend ostream& operator <<( ostream&, const Dog& ); private: char* name; int age; }; Dog.h

2008YeungNam Univ. SE Lab. #include "Dog.h" #include istream& operator>>( istream& is, Dog& d ) { char s[30], c; while ( isspace( is.peek() ) ) is.get(c); // skip leading white spaces is.getline( s, 30, ',' ); is >> d.age; d.name = strdup(s); return is; } ostream& operator<<(ostream& os,const Dog& d) { if ( strlen(d.name) ) os << d.name << ", " << d.age; return os; } Dog.cpp #include "Dog.h" #include using std::ofstream; using std::ifstream; using std::ios; int main() { ofstream osf("C:\\Dog.txt"); if ( !osf ) return 1; Dog d; while ( cin >> d ) osf << d; osf.close(); ifstream isf("C:\\Dog.txt"); if ( !isf ) return 2; while ( isf >> d ) cout << d << '\n'; isf.close(); return 0; } Drv.cpp 15 File I/O 9. File Processing Lucky, 5 Minky, 2 Frisky, 3 ^z Lucky, 5 Minky, 2 Frisky, 3 Press any