Monday, October 2, 2017

Object Oriented Programming Using C++ Chapter No : 10 for BCA and BSc(IT) Studetns


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Mobile No : 7600044051
Email : rupareleducation@gmail.com


Chapter No. 10
Que.    Introduction:
Ans.     Normally the data store in main memory of computer storage but it is not preferable because the size of main memory is usually two small to store all the data to overcome such a problem and store the data permanently in secondary storage device. C++ uses the concept of data file. The data can be store in the device like floppy disk, hard disk etc… file is a collection of related data store in a particular user of the disk. A file is a sequence of bytes, lines or records.

Que.    File stream class.
Ans.     In a C++ the file handling technique is manipulated in the form of stream object.  There are three classes for handling file. That is…

ifstream (input file stream):     it use for handling input files.

ofstream(output file stream):  it use for handling output files.

fstream(file stream):    for handling file on which both input and output can be perform. These classes are derived from fstream base and its header file is iostream.h. The object cin and cout are predefining header file iostream.h to perform standard input and output function. to deal with disk file we need to declare explicitly in the program using the class ifstream, ofstream and fstream. Ifstream support input operations it contain open() function with default input mode. It inharit get() function, getline() function, read() function, seekg() function, tellg() function from iostream. Ofstream support output operation and it contain open() function with default output mode. It inharit put() function, seekp() function, tellp() function and write() function. fstream support input and output operation. It contain open() function with default input and output mode. It inharite all the function from istream and ostream class.

Que.    Opening and closing a file.
Ans.     To access any file first it must be open to get handle it. The file handle and serve the data with a pointer to the file. A file name is string of character by which a file is identify in the programming. To manipulate file involve, naming the file, opening the file. To get the file pointer. Processing the file for reading and writing error check during the process. Closing the file after complete the task / work. To get a file pointer if file does not exists then file must be created and linked to the file name. a file stream can be define using stream class. ifstream, ofstream or fstream. To open a file using constructor or using open method. The constructor method is use to open only one file in the stream either to read, write or in append model. It initialize the file object with the desire file name.

e.x:      ifstream in(“student”);

according to above example:  Here it create in is object is a class of ifstream and it open the file name with student for input perpose. Same as ofstream out(“student”); it create out as the object of ofstream class and open the file student for the output purpose.

Open file using open() Method:

The constructor can open a file as per specified stream class object to perform work either for reading or writing. To provide a facility to open multiple file with the same object open() method can be use.
Following is the syntax of open method: filestream_class , stream_object
                                          Streamobjec.open(filename);

According to above syntax:  first there is a need to create object of any file stream class and then use open() function with file name.
To open any other file with the same stream class object first need to close it. The file can be close using close() function.

Following is a syntax of close() function:

streamobject.close();

According to above syntax: Close the file associated with the specific stream class object.

Que.    File modes:
Ans.     The constructor of ifstram, ofstream and fstream can be open an existing file as per the object of stream class. Same as, open() function can also be use for creating and opening a file. But here, only one argument is specified with constructor and open() function. That is the name of the file. C++ provide a mechanism for opening a file in different mode. For that the open(0 function takes two argument first it’s file name and second specify the file opening mode.

Following is a syntax :           
streamobject.open(“filename”,filemodes);

According to the above syntax: first specify the file name and second argument specify it’s mode.

Following is some list of file modes:

File_modes                                          meaning
1.      ios:in                                              open for reading.
2.      Ios::out                                          opens for writing.
3.      Ios::ate                                          go to the end of the file an opening time.
4.      Ios::app                                         append to end of file.
5.      Ios::trunk                                       truncates the file if it already exists.
6.      Ios::nocreate                                  open fail if file does not exist.
7.      Ios::noreplace                                open fail if file already exists.
8.      Ios::bynary                                    open as a binary file

Que.    File pointers
Ans.     To access the information faster from the store file need to know about logical location at  which the current read or write operation occurs.
The file management system associated with two int pointer with each file its call file pointers.
This two int value are call the get pointer and the put pointer they are also call current get position and current put position.
In short, simply the current position. Here, the get pointer specify a location from where the current leading operation perform.
The put pointer specify a location from where the current writing operation is perform. They both specify the byte number in the file where writing or reading will take place.
File pointer are set to suitable location initially based on the mode in which the file is open. Normally, file can be open in read mode, white mode, append mode.
When a file is open in read only mode the get pointer is initialize to point to the beginning of the file read from the beginning.
When a file is open in write only mode in the case of existing file the existing contain of the file are deleted and output pointer is set to the beginning of the file so that the file can be from the beginning written.
When the file is open in append mode the existing contain of the file remain unaffected in the given file already exists and the output pointer is set to the end of file so that data can be written at the end of the existing file.
The C++ input-output system support a several function for setting a file pointer at any desire position inside the file. They allow the control or a position in the file where the read or write operation can take place.

Following are some functions it support C++ input – output system:
1.      Seekg():     move get file pointer to a specific location. It is the member function of ifstream  class.
2.      Seekp():     move put file pointer to a specific location. It is the member of stream class.
3.      Tellg():       it return the current position of the get pointer. It is the member function of ifstram.
4.      Tellp():       it return the current position of the put pointer. It is a member function of ofstream.

Que.    Sequential input-output operations:
Ans.     A file can be access either sequentially or randomly. In a sequential access the particular data is access in a sequential manner means one by one while random file allow access to the specific data without the need for access its previous data items. However, it can also be access sequentially. Organizing file is actually based on the media on which the file is organize and store. For ex..           a file is store in magnetic tap must be access sequentially. Where as a file on hard disk or floppy disk can be access either sequentially or randomly. C++ file stream support a wide verity of function to perform input and output operation on file. The function put() and get() are design to manage a single other function like, write() and read() are design to manipulate block of characters data.

1.      Put():         The put() function is the member function of the output stream class. It is use to white single character to the output file.
2.      Get():         The get() function is the  member function of the file stream class. It is use to read a single character from the file.
3.      Write():      the write() function is the member function of fstream class and it is use to handle data in binary format. It allow user to write more than one character in a file at a time.
4.      Read():      The read() function is the member function of fstream class. And it is use to handle data in binary format. It allow user to a read more than one character in a file at a time.

Updating a file (Random access) :
The task of writing and reading a data two and from the file can be easily perform by various method. It can be also possible to making, updating the data file.
Following is some updating task:
1.      Adding a new item.
2.      Displaying the content of a file.
3.      Modifying an existing item.
4.      Deleting an existing item.
For performing such action require the file pointer movement to a particular location. That corresponds to the object or modifying a data. If the size of the object item is same then pointer movement can be easily done. The size of each object can be easily measure using the size of operator. So we can update a file or random access possible using a specific function supported by the C++ input output system.

Que.    Error Handling.
Ans.     Some situation may occur during the file access which may cause an unpredictable. The following is some situation may be happen when dealing with the file.
1.      When use try to open a non existing file in a read mode.
2.      When the file name use for new file may already exists.
3.      When user try to open a file in a write mode which can be opened as a read only.
4.      When user try to read the data at the end of the file.
5.      When there not be any space in the disk for storing more data.
6.      When user can invalid file name.
The operation may fail in such a situation for that there is need to provide error handling mechanism. The IOS class maintain and support several function to access the status recorded in the data member. Following are some list of function which help in error handling mechanism.
1.      eof() : This function provide a facility to detect the end of file condition. The end of file function return non zero(0) value means true value. If the eof is encounter otherwise return zero or false.
2.      fail() : It return value if read or write operation has failed other wise false.
3.      bad() : The bad() function return true if an invalid operation is occur.
4.      good() : The good function return true if operation is successful without any error.

Que.    Command line argument.
Ans.     Command line argument is similar to c-language. It can be also possible pass to the main() function. This arguments are passed to the main() function at the time of invocation of program. Normally the arguments are passed from the command prompt so such argument are called command line argument. Following is syntax of the main() function with arguments:
            returntype main(int argc, char *argv[])
            {
                        body of the main() function
}
According to above syntax the first argv, argc specify a total number of arguments in the command line. It is also call argument counter. The second argument argv specify the argument array of character it is also called argument vector. The size of this array will be equal to the value of argc. Command line arguments are type by use from DOS prompt and delimited by space. Here always first argument is the file name which contains the program executable code.