Notes of BE, C & Programming & Coding file-exception.pdf - Study Material
Page 1 :
1, , CAD DESK PLUS INSTITUDE AMRAVATI, , File Handling In C++ Programming, •, , File handling is used to store a data permanently in computer. The data can be, stored in secondary memory (hard disk) using file handling., • The I/O data can easily transfer from one computer to another by using files., • The C++ standard library provides fstream class for performing Read and, Write operations., The fstream defines three new datatypes are as:, Datatype Description, Ofstream→ Used for creating a file and write data on files., Ifstream→ Used for reading the data from files., Fstream→ Used for both read and write data from files., Following are the functions used in File Handling., Function Description, open()→ It is used to create a file., close()→ It is used to close an existing file., get()→ It is used to read a single unit character from a file., put()→ It is used to write a single unit character in file., read()→ It is used to read data from file., write()→ It is used to write data into file., Following are the operations of File Handling., 1. Naming a file, 2. Opening a file, 3. Reading data from file, 4. Writing data into file, 5. Closing a file, Opening a File, • The open() function is used to open multiple files which uses the same stream, object., • The fstream or ofstream object is used to open a file for writing and ifstream, object is used to open a file for reading., Syntax:, void open(const char *filename, ios::openmode mode);, where,, First argument *filename specifies the name of file and location., Second argument open() member function defines the mode in which the, file should be opened., , C++ NOTES BY- ER. RANI THAKARE, , 1
Page 2 :
2, , CAD DESK PLUS INSTITUDE AMRAVATI, , Following are the file opening modes, File mode, Description, ios::out, → This mode is opened for writing a file., ios::in, → This mode is opened for reading a file., ios::app, → This mode is opened for appending data to end-of-file., ios::binary → The file is opened in binary mode., This file moves the pointer or cursor to the end of the file when it, ios::ate, →, is opened., If the file is already exists, using this mode the file contents will, ios::trunc →, be truncated before opening the file., This mode will cause to fail the open() function if the file does not, ios::nocreate →, exists., This mode will cause to fail the open function if the file is already, ios::noreplace→, exists., All these above modes can be combined using the bitwise operator (|). for example,, ios::out | ios::app | ios::binary, Closing a File, • The close() function is used for closing a file., • When a program terminates, it automatically closes or flushes all the streams,, releases all the allocated memory and closes all the opened files. But a good, practice for programmer to close all the opened files before the program, termination., Syntax: void close();, • A file must be closed after completion of all operation related to a file., Writing to a File, • The insertion operator (<<) is used to write information in a file., • The ofstream or fstream object is used instead of cout object., Reading from a File, • The extraction operator (>>) is used to read information from a file into your, program.The ifstream or fstream object is used instead of cin object., Input and Output Operation, Following functions are used for I/O Operation:, Function, Description, put()→ This function writes a single character to the associated stream., get()→ This function reads a single character to the associated stream., write()→ This function is used to write the binary data., read()→ This function is used to read the binary data., C++ NOTES BY- ER. RANI THAKARE, , 2
Page 3 :
3, , CAD DESK PLUS INSTITUDE AMRAVATI, , Exception Handling, What is Exception?, Exception is a problem that occurs during the execution of a program., • It is a response to exceptional circumstances like runtime errors while a, program is running., • To understand the exception firstly we should know about the error in, program can be categorized into two types: - Compile time errors and run, time errors, • Compile time error – Errors caught during compile time is called compile, time errors. compile time errors include library reference, syntax error or, incorrect class import., • Run time errors – The error that occurs during the run time of program is, called run time error. They are also known as exception is a runtime error, that occurs because of user’s mistake., What is Exception Handling?, •, , An exception transfers the control to special functions called Handlers., • Exception handling makes it easy to separate the error handling code., • It provides a way to transfer control from one part of a program to another., • Exception handling makes code readable and maintainable. It separates the, code to the normal flow., • Function can handle or specify any exceptions they choose., Following are the three specialized keywords where exception handling is built., 1. throw, 2. try, 3. catch, throw, •, , •, •, , Using throw statement, an exception can be thrown anywhere within a code, block., It is used to list the exceptions that a function throws, but doesn't handle, itself., , try, •, •, •, , Try represents a block of code that can throw an exception., It identifies a block of code which particular exceptions will be activated., Try block followed by one or more catch blocks., C++ NOTES BY- ER. RANI THAKARE, , 3
Page 4 :
4, , CAD DESK PLUS INSTITUDE AMRAVATI, , Syntax:, try, {, //Code, }, catch(ExceptionName e1), {, //Catch block, }, catch(ExceptionName e2), {, //Catch block, }, catch (ExceptionName e3), {, //Catch block, }, Catch, Catch represents a block of code executed when a particular exception is, thrown., • It follows the try block which catches any exception., Syntax:, •, , try, {, //code, }, catch (ExceptionName e), {, //code to handle exception, }, , C++ NOTES BY- ER. RANI THAKARE, , 4