C++ File Handling 5

Error Handling During File I/O

fail():

this function helps us to check whether a file has been opened successfully or not.

Returns non zero if successful otherwise return zero.

Syntax:
ifstream abc;
abc.open(“filename”);
if(abc.fail())//if(!abc)
{
cout<<”unable to open the file”;
return;
}

where:
ifstream: inbuilt object to open the file in input mode.
abc: user defined object to open the file.
filename: user defined filename to store the data
fail(): inbuilt function check whether the file is opened or not

Example:

Example:
ifstream abc;
abc.open("story.txt");
if(abc.fail())//if(!abc)
{
cout<<”unable to open the file”;
return;
}
abc.close();

eof() :
this function helps us to check whether “End of File” is reached or not. Returns non-zero (true) if end of file is encountered otherwise zero (false) is returned.

Syntax:
ifstream abc;
abc.open(“filename”);
while(!abc.eof())
{
Statements;
}

where:
ifstream: inbuilt object to open the file in input mode.
abc: user defined object to open the file.
filename: user defined filename to store the data
eof(): inbuilt function check whether end of file is reached or not

Example:

ifstream abc;
abc.open("story.txt");
while(!abc.eof())
{
Statements;
}
abc.close();

bad() :
This function helps us to check whether any invalid file operation has been attempted on the file or not.  Returns non-zero (true) if no error has occurred otherwise returns zero(false).

Syntax:
ifstream abc;
abc.open(“filename”);
if (abc.bad())
{
cout<<”File operation failed / invalid operations”;
return;
}
else
{
Statements;
}

where:
ifstream: inbuilt object to open the file in input mode.
abc: user-defined object to open the file.
filename: user-defined filename to store the data
bad(): inbuilt function check whether the operation performed on the file was valid or not

Example:

ifstream abc;
abc.open("story.txt");
if (abc.bad())
{
cout<<”File operation failed / invalid operations”;
return;
}
else
{
Statements;
}
abc.close();

good() :
Helps us to check whether the previous file operation has been successful or not. returns non-zero (true) if no error is occurred otherwise return zero(false).

Syntax:
ifstream abc;
abc.open(“filename”);
if (abc.good())
{
Statements;
}
else
{
cout<<”File operation failed / invalid operations”;
return;
}
where:
ifstream: inbuilt object to open the file in input mode.
abc: user defined object to open the file.
filename: user defined filename to store the data
good(): inbuilt function check whether the operation performed on the file was valid or not

Example:

ifstream abc;
abc.open("filename");
if (abc.good())
{
Statements;
}
else
{
cout<<”File operation failed / invalid operations”;
return;
}
abc.close();

Data manipulations

get():
This function helps us to take read /take input for a single character from the input stream.

Example:

To read a character from the keyboard (Standard input device)

char ch;
cin.get(ch);
ch=cin.get();
cin>>ch;

To read a character from a file “hello”

ifstream abc(“hello”);
abc.get(ch);

To read a character from a file “story.txt”

ifstream abc(“story.txt”);
ch=abc.get();

put() :
this function helps us to display a single character to the output stream.

Example:

To display the character on the screen

cout.put(ch);
cout<<ch;

To write a single character in a file “hello”

char ch=’A’;
ofstream abc(“hello”);
abc.put(ch);

To write a single character in a file “story.txt”

ofstream abc(“story.txt”);
abc.put(‘W’);

getline() :
This function helps us to take input for a group of characters of specified length or till enter is pressed.

Example:

Takes input for first 19 characters (1 location is reserved for NULL character (‘\0’)

char n[20];
cin.getline(n,20);

takes input for first 19 characters (1 location is reserved for NULL character (‘\0’)

Example:

To take input for specific number of characters(4).

cin.getline(n,5);

Will take input for first 4 characters

If we enter “hello” it will store only “hell”

Write() :
this function helps up to display specific number of characters from a specified string.

Syntax:
cout.write(n,3);
Example:
n=”Hello”;
cout.write(n,2);
o/p : He

cout.write(n,3);
o/p : Hel

Handling Records

read() :
this function helps us to read a specific number of bytes form the specified file/stream. This functions helps us to read a record from the file.

ifstream abc;
abc.open(“std”);
student s;
abc.read((char*)&s,sizeof(s));

read()
abc.read((char*)&b1,sizeof(b1));
The read() function reads sizeof(buf) bytes from the associated stream and puts them in the buffer pointed by buf. Helps us to read a record from the file and brings into memory.

write():
This function helps us to write a specific number of bytes to the file/ stream. This helps us to write a record in the file.

ofstream abc;
abc.open(“std”);
student s;
s.get(); //to take input for all the data elements of class
abc.write((char*)&s,sizeof(s));
read() and write() function

write()
abc.write((char*)&b1,sizeof(b1));
The write function writes sizeof(buf) bytes to the associated stream from the buffer pointed to by buf. Helps us to write a record from the memory to the file.
The data written by the write() function can only be read by the read() function.