0% found this document useful (0 votes)
13 views8 pages

Filehandling

OOP

Uploaded by

UMAIR Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Filehandling

OOP

Uploaded by

UMAIR Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

File Handling in C++

Objectives:
In this lab students will learn:

 Read data from files.


 Write and append data on files.

Equipment required:
 Dev-C++/Eclipse/Visual Studio installed in PC/Windows

File Handling
A file is a collection of data that is usually stored on a computer’s disk. Data can be saved to
files and then later reused. The information/data stored under a specific name on a storage
device, is called a file. Without file handling whatever the program does is temporary and once
we stop the program all the memory vanishes. We can store it by writing our data on a file so
we can reuse that data next time.
The following data types are used for file handling operation.

ifstream: Input File Stream: This data type can be used only to read data from files into
memory.
ofstream: Output File Stream: This data type can be used to create files and write data to
them.
fstream: File Stream: This data type can be used to create files, write data to them, and read
data from them.
Using the fstream data type we define an fstream object just as we define objects of other
data types. The following statement defines an fstream object named dataFile.
fstream dataFile;

Opening of the file


We use an fstream object’s ‘open’ function to open a file. An fstream object’s ‘open’ function
requires two arguments. The first argument is a string containing the name of the file. The
second argument is a file access flag that indicates the mode in which you wish to open the
file. Here is an example.
dataFile.open("info.txt", ios::out);

The first argument in this function call is the name of the file, info.txt. The second argument is
the file access flag ios::out . This tells C++ to open the file in output mode. Output mode allows
data to be written to a file. The following statement uses the ios::in access flag to open a file
in input mode, which allows data to be read from the file.

dataFile.open("info.txt", ios::in);
NOTE: When used by itself, the ios::out flag causes the file’s contents to be deleted if the
file already exists. When used with the ios::in flag, however, the file’s existing contents are
preserved. If the file does not already exist, it will be created.

Example
This program uses an fstream object to write data to a file.

Example OUTPUT

The getline Function


The problem with above Program can be solved by using the getline function. The function
reads a “line” of data, including whitespace characters. Here is an example of the function call:
getline(objFile, str,'\n');
The three arguments in this statement are explained as follows:

objFile:
This is the name of the file stream object. It specifies the stream object from which the data is
to be read.
str:
This is the name of a string object. The data read from the file will be stored here.
' \ n':
This is a delimiter character of your choice. If this delimiter is encountered, it will cause the
function to stop reading. (This argument is optional. If it’s left out, ' \ n' is the default.) The
statement is an instruction to read a line of characters from the file. The function will read until
it encounters a \n. The line of characters will be stored in the str object.
Example
If you want to get the data line wise or you want to get the count of lines then use getline.
This program will print the lines written in file and its count by using getline function.

Output:
Example:

Below is the implementation of extraction operator >> while reading data from file. It is used to
read data from file. If we use this operator with string then it will give us the count of word (one
string will be read and displayed if needed.

Output:
Example:
For reading alphabets and getting their count, the following program is implemented

Output:
Example:
Take values from user and store in a file named city.txt.

#include <iostream>
#include <fstream>
#include<conio.h>
#include <string.h>
using namespace std;
int main()
{
//Write a program that inputs 5 names of citie and store them in a file city.txt
char names[15];
ofstream write_file("city.txt");
if(!write_file)
{
cout<<"File Opening Error";
exit(1);
}

for(int i=0; i<5;i++)


{
cin>>names;
write_file<<names<<endl;
}

write_file.close();

ifstream read_file("city.txt");
cout<<endl;
cout<<"The cities stored in file are:";
read_file>>names;
while(!read_file.eof())
{

cout<<names<<endl;
read_file>>names;
//read_file.getline(n,10);
}
read_file.close();
}
Checking for a File’s Existence
Before Opening the file sometimes, we want to determine whether a file already exists before
opening it for output. We can do this by first attempting to open the file for input. If the file does
not exist, the open operation will fail. In that case, you can create the file by opening it for
output. The following code gives an example.
Example
1. Lab Tasks

1. Write a code that reads the contents of one .txt file (created manually) in the code and
write those in another .txt file.

You might also like