計算機概論實習 2007-05-25. 2 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker 03124589... n-1 end-of-file marker 67 This is.

Slides:



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

STARTING OUT WITH STARTING OUT WITH Class 15 Honors.
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
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.
計算機概論實習 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using.
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.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 14 – File Processing Outline 14.1Introduction 14.2The Data Hierarchy 14.3Files and Streams.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
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,
File I/O ifstreams and ofstreams Sections 11.1 &
Programming Principles II Lecture Notes 7 Files Andreas Savva.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
 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.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
計算機程式語言 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.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
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.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
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
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
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.
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)
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.
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
File Processing.
File Processing.
Programming with ANSI C ++
17 File Processing.
Introduction to Programming
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.
C ++ MULTIPLE CHOICE QUESTION
CPSC 231 D.H. C++ File Processing
17 File Processing.
Basic File I/O and Stream Objects
17 File Processing.
آشنایی با ساختارها و کار با فایلها
Chapter 13: Advanced File and I/O Operations
Topics Input and Output Streams More Detailed Error Testing
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
C++ Programming Lecture 8 File Processing
Reading from and Writing to Files
Presentation transcript:

計算機概論實習

2 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 a test HAHA!

3 File Processing include and To open file, create objects Creates "line of communication" from object to file Classes ifstream (input only) ofstream (output only) fstream (I/O) Constructors take file name and file-open mode ofstream outClientFile( "filename", fileOpenMode ); To attach a file later Ofstream outClientFile; outClientFile.open( "filename", fileOpenMode);

4 File-open Modes ModeDescription ios::appWrite all output to the end of the file. ios::ateOpen a file for output and move to the end of the file (normally used to append data to a file). Data can be written anywhere in the file. ios::inOpen a file for input. ios::outOpen a file for output. ios::trunc Discard the file ’ s contents if it exists (this is also the default action for ios::out) ios::binaryOpen a file for binary (i.e., nontext) input or output. ofstream outClientFile( "clients.dat", ios::out ); ofstream outClientFile( "clients.dat");

5 Operations Writing to file (just like cout ) outClientFile << myVariable Closing file outClientFile.close() Automatically closed when destructor called Reading from files inClientFile >> myVariable Stops when EOF found (gets value 0 )

6 Example – Write to File #include #include // exit prototype using namespace std; int main() { // ofstream constructor opens file ofstream outClientFile( "clients.dat", ios::out ); // exit program if unable to create file if ( !outClientFile ) { // overloaded ! operator cout << "File could not be opened" << endl; exit( 1 ); } // end if

7 Example – Write to File cout << "Enter the account, name, and balance." << endl << "Enter end-of-file to end input.\n "; int account; char name[ 30 ]; double balance; // read account, name and balance from cin, then place in file while ( cin >> account >> name >> balance ) { outClientFile << account << ' ' << name << ' ' << balance << endl; cout << "? "; } // end while return 0; // ofstream destructor closes file } // end main

8 Example – Write to File Enter the account, name, and balance. Enter end-of-file to end input. ? 100 Jones ? 200 Doe ? 300 White 0.00 ? 400 Stone ? 500 Rich ? ^Z

9 Example – Read from File #include #include // exit prototype using namespace std; int main() { // ifstream constructor opens the file ifstream inClientFile( "clients.dat", ios::in ); // exit program if ifstream could not open file if ( !inClientFile ) { cout << "File could not be opened" << endl; exit( 1 ); } // end if

10 Example – Read from File int account; char name[ 30 ]; double balance; cout << left << setw( 10 ) << "Account" << setw( 13 ) << "Name" << "Balance" << endl << fixed << showpoint; // display each record in file while ( inClientFile >> account >> name >> balance ) { cout << left << setw( 10 ) << account << setw( 13 ) << name << setw( 7 ) << setprecision( 2 ) << right << balance << endl; } return 0; // ifstream destructor closes the file } // end main

11 Example – Read from File Account Name Balance 100 Jones Doe White Stone Rich

12 File Position Pointers Number of next byte to read/write Functions to reposition pointer seekg (seek get for istream class) seekp (seek put for ostream class) Classes have "get" and "put" pointers seekg and seekp take offset and direction Offset: number of bytes relative to direction Direction ( ios::beg default) ios::beg - relative to beginning of stream ios::cur - relative to current position ios::end - relative to end

13 Examples fileObject.seekg(0) Goes to front of file (location 0 ) because ios::beg is default fileObject.seekg(n) Goes to nth byte from beginning Example: n = 8 This is a test HAHA! This is a test HAHA!

14 Examples fileObject.seekg(n, ios::cur) Goes n bytes forward Example: ios:cur = 8, n = 2 fileObject.seekg(0, ios::cur) Goes to last byte This is a test HAHA!

15 Examples fileObject.seekg(y, ios::end) Goes y bytes back from end seekp similar

16 Practice7 (P7) A student file stores the student ’ s name and the grade of Chinese, Mathematics, and English. Please provide a program that read the content of student file and print out. Then after computing the average grade, you must store all the information in another file. The information contains student ’ s name, the grade of the three courses, and finally the average grade.

17 Example – Read File The content student file Judy May Gary To print above information on the screen 姓名 國文 英文 數學 Judy May Gary

18 Example – Write to File Store all information in another file (e.g. student-1.txt) Judy May Gary