dct1063 programming 2 chapter 3 file input and file output

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

Upload: quynn-hammond

Post on 02-Jan-2016

36 views

Category:

Documents


1 download

DESCRIPTION

DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT. Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College [email protected]. Objectives. After completing this chapter student should be able to:-. - PowerPoint PPT Presentation

TRANSCRIPT

DCT1063 Programming 2

CHAPTER 3 FILE INPUT AND FILE OUTPUT

Mohd Nazri Bin IbrahimFaculty of Computer, Media & Technology

TATi University [email protected]

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 <fstream>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 <iostream>#include <fstream>using namespace std;int main(){ofstream out("test");if(!out) {cout << "Cannot open file.\n";return 1;}

out << 10 << " " << 123.23 << "\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 << and >> 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 <iostream>#include <fstream>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 <iostream>

#include <fstream>

using namespace std;

int main(int argc, char *argv[])

{

char ch;

if(argc!=2) {

cout << "Usage: PR <filename>\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 reachedin.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 <iostream>• #include <fstream>• 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 <iostream>#include <fstream>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 arrayn[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 filecout << 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 <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){char ch;if(argc!=2) {cout << "Usage: PR <filename>\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 <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){register int i;unsigned char buf1[1024], buf2[1024];if(argc!=3) {cout << "Usage: compfiles <file1> <file2>\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 buffersfor(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 <iostream>#include <fstream>#include <cstdlib>using namespace std;int main(int argc, char *argv[]){if(argc!=3) {cout << "Usage: CHANGE <filename> <byte>\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 <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])

{

char ch;

if(argc!=3) {

cout << "Usage: NAME <filename> <starting location>\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 <fstream> 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.