DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Slides:



Advertisements
Similar presentations
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.
Advertisements

 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.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
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.
 2008 Pearson Education, Inc. All rights reserved File Processing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
“After a day spent staring at a computer monitor, think of a book as a kind of screen saver for your brain” “One good reason why computers can do more.
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++. 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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 Handling in C++.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
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.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Starting Out with C++ 1 Chapter 12: File Operations What is a File? A file is a collection on information, usually stored on some electronic medium. Information.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
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
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
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.
Basic Input and Output Operations
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
17 File Processing.
17 File Processing.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
Chapter 13: Advanced File and I/O Operations
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
C++ Programming: chapter 6 – iostream
Presentation transcript:

DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Objectives After completing this chapter student should be able to:- Create, read, write and update files ( Sequential file processing) Random-access file processing) Use high-performance unformatted I/O operations. Differentiate between formatted-data and raw-data file processing.

File I/O You can use the C++ I/O system to perform File Input and File Output ( I/O). The header defines several important classes and values for File Input and File Output.

Opening and Closing a File A file is opened by linking it to a stream. There are three types of streams: 1.input, 2.output, and 3.input/output. To open an input stream, you must declare the stream to be of class ifstream. To open an output stream, it must be declared as class ofstream. A stream that will be performing both input and output operations must be declared as class fstream.

Opening and Closing a File(2) This fragment creates one input stream,one output stream, and one stream capable of both input and output: ifstream in; // input ofstream out; // output fstream both; // input and output Once you have created a stream, one way to associate it with a file is by using open( ). ofstream out; out.open("test"); –For short, you can write ofstream out("test");

Opening and Closing a File(3) If open( ) fails, the stream will evaluate to false when used in a Boolean expression. You can make use of this fact to confirm that the open operation succeeded. if(!mystream) { cout << "Cannot open file.\n"; // handle error } You can also check to see if you have successfully opened a file by using the is_open( ) function,

Opening and Closing a File(4) if(!mystream.is_open()) { cout << "File is not open.\n"; To close a file, use the member function close( ). mystream.close(); The close( ) function takes no parameters and returns no value.

Reading and Writing Text Files // this program writes an integer, a floating-point value, and a string to a file called test: #include using namespace std; int main() { ofstream out("test"); if(!out) { cout << "Cannot open file.\n"; return 1; } out << 10 << " " << << "\n"; out << "This is a short text file."; out.close(); return 0; } The easiest way to read from or write to a text file is to use the > operators.

Reading and Writing Text Files(2) The following program reads an integer, a float, a character, and a string from the file created by the previous program: #include using namespace std; int main() { char ch; int i; float f; char str[80]; ifstream in("test"); if(!in) { cout << "Cannot open file.\n"; return 1; } in >> i; in >> f; in >> ch; in >> str; cout << i << " " << f << " " << ch << "\n"; cout << str; in.close(); return 0; }

Reading and Writing Text Files(3) When the >> operator is used for reading text files, certain character translations occur. Whitespace characters are omitted. To prevent any character translations, you must open a file for binary access. Also remember that when >> is used to read a string, input stops when the first whitespace character is encountered.

Unformatted Binary I/O Formatted text files (like those used in the preceding examples) do not have the flexibility of unformatted binary files. C++ supports a number of binary (sometimes called “raw”) file I/O functions that can perform unformatted operations. Use the ios::binary mode specifier to perform binary operations. There are two ways to write and read unformatted binary data to or from a file. 1.function put( ) to write, function get( ) to read. 2.functions write( ) to write, function read( ) to read.

Unformatted Binary I/O(2) Using get( ) and put( ) The get( ) function reads a single character from the associated stream and puts that value in ch. This value will be null if the end of the file is reached. The put( ) function writes ch to the stream and returns a reference to the stream. The following program will display the contents of any file on the screen. It uses the get( ) function.

// Display a file using get(). #include using namespace std; int main(int argc, char *argv[]) { char ch; if(argc!=2) { cout \n"; return 1; } ifstream in(argv[1], ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } while(in) { // in will be false when eof is reached in.get(ch); if(in) cout << ch; } in.close(); return 0; } When in reaches the end of the file, it will be false, causing the while loop to stop.

This program uses put( ) to write a string to a file: // Use put() to write to a file. #include using namespace std; int main() { char *p = "hello there"; ofstream out("test", ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n"; return 1; } while(*p) out.put(*p++); out.close(); return 0; }

Reading and Writing Blocks of Data To read and write blocks of binary data, use the read( ) and write( ) member functions. Their prototypes are shown here: –istream &read(char *buf, streamsize num); –ostream &write(const char *buf, int streamsize num); The read( ) function reads num bytes from the associated stream and puts them in the buffer pointed to by buf. The write( ) function writes num bytes to the associated stream from the buffer pointed to by buf.

Reading and Writing Blocks of Data(2) streamsize is defined as some form of integer. The following program writes and then reads an array of integers: Note that the type casts inside the calls to read( ) and write( ) are necessary when operating on a buffer that is not defined as a character array.

// Use read() and write(). #include using namespace std; int main() { int n[5] = {1, 2, 3, 4, 5}; register int i; ofstream out("test", ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n"; return 1; } out.write((char *) &n, sizeof n); out.close(); for(i=0; i<5; i++) // clear array n[i] = 0; ifstream in("test", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } in.read((char *) &n, sizeof n); for(i=0; i<5; i++) // show values read from file cout << n[i] << " "; in.close(); return 0; }

Detecting EOF You can detect when the end of the file is reached by using the member function eof( ), which has this prototype: bool eof( ); It returns true when the end of the file has been reached; otherwise, it returns false. The following program uses eof( ) to display the contents of a file on the screen:

// Detect end-of-file using eof(). #include using namespace std; int main(int argc, char *argv[]) { char ch; if(argc!=2) { cout \n"; return 1; } ifstream in(argv[1], ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } while(!in.eof()) { // use eof() in.get(ch); if(!in.eof()) cout << ch; } in.close(); return 0; }

A File Comparison Example The following program illustrates the power and simplicity of the C++ file system. It compares two files for equality. It does so by using the binary file functions read( ), eof( ), and gcount( ). gcount( ) function used to determine precisely how many characters are in the buffers. The program first opens the files for binary operations. Next, it reads one buffer at a time from each of the files and compares the contents.

// Compare files. #include using namespace std; int main(int argc, char *argv[]) { register int i; unsigned char buf1[1024], buf2[1024]; if(argc!=3) { cout \n"; return 1; } ifstream f1(argv[1], ios::in | ios::binary); if(!f1) { cout << "Cannot open first file.\n"; return 1; } ifstream f2(argv[2], ios::in | ios::binary); if(!f2) { cout << "Cannot open second file.\n"; return 1; }

cout << "Comparing files...\n"; do { f1.read((char *) buf1, sizeof buf1); f2.read((char *) buf2, sizeof buf2); if(f1.gcount() != f2.gcount()) { cout << "Files are of differing sizes.\n"; f1.close(); f2.close(); return 0; } // compare contents of buffers for(i=0; i<f1.gcount(); i++) if(buf1[i] != buf2[i]) { cout << "Files differ.\n"; f1.close(); f2.close(); return 0; } } while(!f1.eof() && !f2.eof()); cout << "Files are the same.\n"; f1.close(); f2.close(); return 0; }

Random Access So far, files have always been read or written sequentially. You can also access a file in random order. you perform random access using the seekg( ) and seekp( ) functions. Their most common forms are shown here: –istream &seekg(off_type offset, seekdir origin); –ostream &seekp(off_type offset, seekdir origin); off_type is an integer type defined by ios that is capable of containing the largest valid value that offset can have.

Random Access(2) seekdir is an enumeration that has these values: The C++ I/O system manages two pointers associated with a file.

Random Access(3) One is the get pointer, which specifies where in the file the next input operation will occur. The other is the put pointer, which specifies where in the file the next output operation will occur. Using the seekg( ) and seekp( ) functions, it is possible to access the file in a non- sequential fashion. Generally, random access I/O should be performed only on those files opened for binary operations.

Random Access(4) The following program demonstrates the seekp( ) function. // Demonstrate random access. #include using namespace std; int main(int argc, char *argv[]) { if(argc!=3) { cout \n"; return 1; } fstream out(argv[1], ios::in | ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n"; return 1; }

out.seekp(atoi(argv[2]), ios::beg); out.put('X'); out.close(); return 0; }

The next program uses seekg( ). // Display a file from a given starting point. #include using namespace std; int main(int argc, char *argv[]) { char ch; if(argc!=3) { cout \n"; return 1; }

ifstream in(argv[1], ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n"; return 1; } in.seekg(atoi(argv[2]), ios::beg); while(in.get(ch)) cout << ch; return 0; }

You can determine the current position of each file pointer by using these functions: pos_type tellg( ); pos_type tellp( );

Summary C++ views each file as a sequential stream of bytes. Each file ends either with an end-of-file marker or at a specific byte number recorded in a system maintained, administrative data structure. Header includes the definitions for the stream class templates basic_ifstream (for file input), basic_ofstream (for file output) and basic_fstream (for file input and output). For an ofstream object, the file-open mode can be either ios::out to output data to a file or ios::app to append data to the end of a file (without modifying any data already in the file).

Summary(2) The file-open mode ios::ate opens a file for output and moves to the end of the file. This is normally used to append data to a file, but data can be written anywhere in the file. Existing files opened with mode ios::out are truncated (i.e., all data in the file is discarded). By default, ofstream objects are opened for output. The ofstream member function open opens a file and attaches it to an existing ofstream object.

Summary(3) You can use the ofstream member function close to close the ofstream object explicitly. Both istream and ostream provide member functions for repositioning the file-position pointer (the byte number of the next byte in the file to be read or written). These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream. The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.

Summary(4) Member functions tellg and tellp are provided to return the current locations of the "get" and "put" pointers, respectively. Individual records of a random-access file can be accessed directly (and quickly) without the need to search other records.