C++ Notes Unit 5
C++ Notes Unit 5
What is a File?
A file is a collection of data stored in a particular area on the disk. Programs are
designed to perform read and write operations on these files.
Types of files:
A file can be Two types.
1. Text File
2. Binary File
By default files in C++ are considered as Text Files.
1. Text file:-
Text File stores information in ASCII characters.
In text files, each line of text is terminated with a special character known as EOL
(End of Line) character .
2. Binary file.
Binary File stores information in Binary Format.
Binary files are faster and easier for programs to read & write.
List out and explain Classes for File stream operations in C++.
In C++, file streams are used for input and output operations on files.
The standard file stream classes are stored in <fstream> header file.
The following Figure shows the file stream class hierarchy in C++.
1
In C++, files are mainly dealt by using below three classes, ifstream, ofstream, fstream
available in fstream header file.
1. ifstream
2. ofstream
3. fstream
1. ifstream:-
ifstream class is used to read the information from a file. It contains open() function
with default input mode.
Example:
ifstream object_name;
ifstream obj;
To read File use : ios::in
obj.open("rani.txt",ios::in);
2. ofstream
ifstream class is used to write information into file. It contains open() function with
default output mode.
2
Example:
ofstream object_name;
ofstream obj;
To read File use : ios::out
obj.open("rani.txt",ios::out);
3. fstream:
fstream class provides support for simultaneous input and output operations ie read &
write operation. Inherits all the functions from istream and ostream classes through
iostream.
Example:
fstream object_name;
fstream obj;
4. filebuf:
Example:
fstream fs(“tseek.data”, ios::in);
filebuf* fb = fs.rdbuf();
fb = fs.rdbuf();
3
How do you open and Close a File in C++?
To open a file, a file stream is created and then it is linked to the filename.
ii. Open File Using the member function open() of the class
The constructors of stream classes (ifstream, ofstream, or fstream) are used to initialize
file stream objects with the filenames passed to them.
filestream object_name("File_Name",ios::in);
fin.close();
Here "ifstream" is the stream class, "myfile" is the file name and "fin" is the object name,
fin.close() is used to close the file.
Close()
Close() function is used to close a file.
Example:
4
Discuss about different types of File Modes in C++.
5
File Handling functions in C ++
The while(fin.get(ch)) - while loop terminates when if(fin.eof()) - fin object returns a
value of zero or reaching at teh end-of-file.
6
Example Program for EOF
First of all, create a text file, “example.txt”. Add the following content ("Hello How are
you") to it and then save the file in Bin Folder.
Coding:
#include<iostream.h>
#include<conio.h>
#include <fstream.h>
void main()
{
clrscr();
fstream fin("example.txt", ios::in) ;
char ch;
while (fin.get(ch))
cout << ch;
if (fin.eof())
cout << "[EoF reached]\n";
else
cout << " Error Reading]\n";
fin.close();
getch();
}
7
Discuss about Files Pointers and their Manipulations in C++.
1. Input Pointer:
When we open a file in read only mode (ios::in), the input pointer automatically set at
the beginning of the file. So that we can read the file from starting position.
Output Pointer:
When we open file in write only mode (ios::out), the exsisting contents are deleted and
the output pointer is set at the beginning of the file. This enables us to write the file from
starting point of the file.
In Uppend Mode:
In case we want to open an existing file to add more data, the file is opened in "append"
mode which is move the file pointer at the end of the file.
8
Functions for Manipulation of File Pointer:
The File Stream Classes provides the following Four Functions to control & manipulate
the move of the get & put pointers.
1. Seekg()
2. Seekp()
3. tellg()
4. tellp()
1. Seekg()
It places the file pointer to the specified position in input mode of file.
Example:
file.seekg(4,ios::beg);
2. Seekp()
It places the file pointer to the specified position in output mode of file.
Example:
file.seekp(position -2);
3. tellg()
It Gives the current position of the get pointer in input mode of file.
Example:
int p=file.tellg();
cout<<"Tellg outpult:"<<p;
4. tellp()
Example:
It Gives the current position of the put pointer in output mode of file.
int position=file.tellp();
cout<<position;
void main()
{
clrscr();
char name[80];
fstream file;
9
file.open("College",ios::out);
cout<<"Enter Name: ";
cin>>name;
file<<name;
file.close();
file.open("College",ios::in);
char c;
file.seekg(4,ios::beg);
file.get(c);
cout<<"Bye 3 from begining :"<<c;
int p=file.tellg();
cout<<"Tellg outpult:"<<p;
file.close();
getch();
}
Output:
void main()
{
clrscr();
int position;
fstream file;
file.open("testing.txt",ios::out);
file.write("Hello",5);
position=file.tellp();
cout<<"Current Poisition of pointer is:"<<position<<endl;
file.seekp(position -2);
file<<"Bye";
getch();
}
10
Output:
File Output:
Example:
if (!outfile.is_open())
{
cout << "Error: Could not open the file for writing!" << endl;
11
2. Read / Write Error Handling:
Check Read/Write Errors after opening a file successfully using fail() function.
if (outfile.fail())
{
void main() {
ifstream file("Student_info.txt");
Output:
Error: File could not be opened!
The if(!file) condition check, whether the file is exists or not in Bin Directory. If the file does not exist,
then it will display the error.
12
Discuss Command-Line Arguments in C++.
Command-line arguments allow users to pass input values to a C++ program when it is
executed. These arguments are passed through the terminal or command prompt.
In C++, the main() function can take two parameters to handle command-line
arguments:
argc (Argument Count): Stores the number of arguments passed, including the program
name.
argv (Argument Vector): An array of character pointers (char*), where each element
represents a command-line argument.
Example Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
return 0;
}
13
14