Object Oriented Programming: Chapter 6: IO Systems and File IO

Error Handling in File I/O with Member Function

C++ Object Oriented Programming

When error occurs in file handling,. flags are set in the state according to the general category of the error.

Error Handling in File I/O with Member Function

•  When error occurs in file handling,. flags are set in the state according to the general category of the error. Flags and their error categories are summarized in the following table.

 

state flag    :      Purpose

ios::goodbit  ‒    This state flag indicates that there is no error with streams. In this case the status variables has value 0.

ios::badbit   ‒    This state flag indicates that the stream is corrupted and no read/write operation can be performed.

ios::failbit    ‒   This state flag indicates that input/output operation failed.

ios::eofbit   ‒     This state flag indicates that the input operation reached end of input sequence.

 

•  When error occurs while performing file I/O operations, the appropriate message is displayed and then the file terminates.

•  Following program illustrates how to use appropriate error messages corresponding read and write error‒causing situations.

#include <iostream>

#include <fstream>

using namespace std;

#include <process.h>

const int MAX = 10;

int array1[MAX] { 10,20,30,40,50 };

int array2[MAX];

int main()

 {

ofstream os; //create output stream

os.open("d:\\test.dat", ios::trunc | ios::binary);//Opening file

if (!os)

{

     cerr << "Could not open output file\n"; //Error Handling

     exit(1);

}

cout << "Writing the contents to the file...\n\n";

os.write((char*)&array1,sizeof(array1));//writing 'array1' to file

if (!os)

{

     cerr << "Could not write to file\n"; //Error handling

     exit(1);

os.close(); //close the file

ifstream is; //create input stream for reading the contents from file

is.open("d:\\test.dat", ios::binary);

if (!is)

{

     cerr << "Could not open input file\n"; //Error Handling

     exit(1);

}

cout << "Reading the contents from the file ...\n";

is.read((char*)&array2,sizeof(array2)); //reading the contents in another array

                                                           //'array2'

if (!is)

{

     cerr << "Could not read from file\n"; //Error Handling

     exit(1);

}

for (int j = 0; j < MAX; j++) //check data

     cout << " " << array2[j];

return 0;

}

Output

Writing the contents to the file...

Reading the contents from the file…

10 20 30 40 50 0 0 0 0 0

Program explanation: In above program we have created one array of integers namely array1. Then in the test.dat file the contents of this array are written. Again the file is opened in read mode and contents of the file are read in another array namely array2 and then those contents are displayed on the console.

During, open, read and write modes of the file, appropriate error handling is done. Using the cerr the error messages can be displayed.

The member functions used for Error Handling.

Function        Purpose

good()     ‒       It returns true if no error.

eof()       ‒       It reruns true if end of file is reached.

fail().      ‒      It returns true if logical error occurs

bad()      ‒       It returns true if serious I/O errors

clear()    ‒      It resets all error flags.

Step 1: Create a file named data.txt

10

20

30

Hello

50

Step 2: Create a C++ Program to read the above input file

#include<iostream>

#include<fstream>

using namespace std;

int main() {

ifstream file("data.txt");

int number;

cout<<"Program for reading a number: "<<endl;

while(true){

file>>number;

if(file.eof()){

     cout<<"Reached End of File!!!";

     break;

}

else if(file.fail()){

     cout<<"Error: Wrong data type encountered "<<endl;

     file.clear();

}

else if(file.bad()){

     cout<<"Fatal Error Encountered";

     break;

}

else {

     cout<<number<<endl;

}

}

file.close();

return 0;

}

Output

Program for reading a number:

10

20

30

Error: Wrong data type encountered

 

Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Error Handling in File I/O with Member Function


Object Oriented Programming: Chapter 6: IO Systems and File IO



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation