Chapter 3 File Management PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an overview of file management in C++. It details file handling techniques, including the use of streams and file pointers. Topics are explained using examples. It is suitable for undergraduates studying computer science.
Full Transcript
CHAPTER THREE FILE MANAGEMENT File Management File handling is an important part of all programs. Most of the applications will have their own features to save some data to the local disk and read data from the disk again. Files which are on the secondary storage device are called physic...
CHAPTER THREE FILE MANAGEMENT File Management File handling is an important part of all programs. Most of the applications will have their own features to save some data to the local disk and read data from the disk again. Files which are on the secondary storage device are called physical files. In order to process file through program, logical file must be created on the RAM. This logical file is nothing but an object having file data type. As an object there should be a variable identifier that points to it. This variable is called file variable and some times also called file handler. C++ File I/O classes simplify such file read/write operations for the programmer by providing easier to use classes. Streams and Files The I/O system supplies a consistent interface to the C++ programmer independent of the actual device being accessed. This provides a level of abstraction between the programmer and the device. This abstraction is called stream. The actual device is called a file. Streams The C++ file system is designed to work with a wide variety of devices, including terminals, disk drives, and tape drives. Even though each device is very different, the C++ file system transforms each into a logical device called stream. There are two types of streams: text and binary. Text Streams A text stream is a sequence of characters. In a text stream, certain character translations may occur as required by the host environment. For example a new line may be converted to a carriage return/linefeed pair. There may not be a one-to-one relationship between the characters that are written (or read) and those on the external device. Because of possible transformations, the number of characters written (or read) may not be the same as those on the external device. Binary streams A binary stream is a sequence of bytes with a one-to-one correspondence to those in the external device i.e., no character translations occur. The number of bytes written (or read) is the same as the number on the external device. However, an implementation-defined number of null bytes may be appended to a binary stream. These null bytes might be used to pad the information so that it fills a sector on a disk, for example. Files In C++, a file can be anything from a disk file to a terminal or printer. You associate a stream with a specific file by performing an open operation. Once a file is open, information can be exchanged between it and a program. All streams are the same but all files are not. If the file can support position requests, opening that file also initializes the file position indicator to the start of the file. As each character is read from or written to the file, the position indicator is incremented. You disassociate a file from a specific stream with a close operation. If you close a file opened for output, then contents, if any, of its associated stream are written to the external device. -- this process is referred to as flushing the stream. All files are closed automatically when the program terminates normally. Files are not closed when a program terminates abnormally. Each stream that is associated with a file has a file control structure of type FILE. This structure FILE is defined in the header stdio.h. The File Pointer A file pointer is a pointer to information that defines various things about the file, including its name, status, and the current position of the file. In essence, the file pointer identifies a specific disk file and is used by the associated stream to direct the operation of the I/O functions. A file pointer is a pointer variable of type FILE. FILE * fp; The standard streams When ever a program starts execution, three streams are opened automatically. stdin --- standard input. stdout -- standard output stderr -- standard error Normally, these streams refer to the console. Because the standard streams are file pointers, they can be used by the ANSI C file system to perform I/O operations on the console. C++ File I/O Classes and Functions To perform file I/O, the header file fstream.h is requied. fstream.h defines several classes, including ifstream, ofstream, and fstream. These classes are derived form istream and ostream, repectively. istream and ostream are derived form ios. Three file I/O classes are used for File Read/Write operations: ifstream - Can be used for File read/input operations ofstream - Can be used for File write/output operations fstream - Can be used for both read/write c++ file I/O operations These classes are derived directly or indirectly from the classe istream, and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. Therefore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files. Let's see an example: Text and Binary Files In file processing, files are generally classified into two as Text file and Binary file Text file is a file in which its content is treated as a sequence of characters and can be accessed sequentially. Where as binary file is a file in which its content is treated as record sequence in a binary format. Binary format refers to the actual format the data is going to be placed and processed in the memory which is directly related to its data type. For example, the value int count 321 will be stored in three byte if it is written in text file considering the digit sequence ‘3’, ‘2’, ‘1’. It will be stored in two byte if it is written in binary file since int requires two byte to store any of its value. When you open the binary file you will see the character equivalence of the two bytes. 321 in binary equals 0000 0001 0100 0001 The first byte holds the character with ASCII value equals to one and the second byte a character with ASCII value equals 65 which is ‘A’. Then if you open the binary file you will see these characters in place of 321. Text File processing File processing involves the following major steps Declaring file variable identifier Opening the file Processing the file Closing the file when process is completed. Opening and Closing a file An open file is represented within a program by a stream object and any input or output operation performed on this stream object will be applied to the physical file associated to it. In C++, you open a file by linking it to a stream. Before you can open a file, you must first obtain a stream. There are three types of streams: input, output, and input/output. To create an input stream, you must declare the stream to be of class ifstream. To create an output stream, you must declare it as class ofstream. Streams that will be performing both input and output operations must be declared as class fstream. ifstream in ; //input stream ofstream out ; // output stream fstream io ; // input and output Once you have declared a stream, you associate it with a file by using the method open(). The method open ( ) is a member of each of the three stream classes. Its prototype is: void open (const char *filename, int mode, int access = filebuf::penprot ); Where: Filename is the name of the file. The value of the mode determines how the file is opened. It must be one (or more) of these values: Mode Description ios::app Write all output to the end of the file ios::ate Open 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::binary Cause the file to be opened in binary mode. ios::in Open a file for input ios::nocreat If the file does not exist, the open operation fails. e ios::norepla If the file exists, the open operation fails. ce ios::out Open a file for output ios:trunc Discard the file's content if it exists (this is also the default action ios::out) You can combine two or more of these values by using them together. ofstream out ; out.open ( "test", ios::out); // correct statement ofstream out1 ; out.open ( " test"); // the default value of mode is ios::out – // correct statment To open a stream for input and output, you must specify both the ios::in and the ios::out mode values. (No default value for mode is supplied in this case.) fstream myStream; myStream.open ( "test", ios::in | ios::out ); If open ( ) fails, myStrream will be zero if (myStream){ cout