0% found this document useful (0 votes)
4 views

Unit-6 Managing IO Formats and Operations

The document discusses the fundamentals of file handling in C++, emphasizing the importance of storing data permanently beyond program termination. It introduces the fstream library, which includes classes for creating, reading, and writing files, and outlines the basic file operations such as opening, reading, writing, and closing files. Additionally, it provides syntax examples for performing these operations using ofstream and ifstream objects.

Uploaded by

lakshrajsinh188
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit-6 Managing IO Formats and Operations

The document discusses the fundamentals of file handling in C++, emphasizing the importance of storing data permanently beyond program termination. It introduces the fstream library, which includes classes for creating, reading, and writing files, and outlines the basic file operations such as opening, reading, writing, and closing files. Additionally, it provides syntax examples for performing these operations using ofstream and ifstream objects.

Uploaded by

lakshrajsinh188
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BCA Semester - II

CA129 Fundamentals of Object Oriented Programming

Unit-6 Managing IO Formats and Operations

Introduction:

Until now we are using Console Oriented Input/output System. So using Console Application, when a
Program is terminated, the Program data stored in main memory is lost. For Small Program like calculator,
we don't need to store data permanently. but what about the large programs?

Whenever we create a Program, and enter any data to process, after the program is terminated, the data
stored in the memory will lost. So to store data Permanently, C++ provide File handling.

File:

File is a collection of bytes that is stored on secondary storage devices like disk.

File Handling:

File Handling provides a mechanism to store output of a program in a file and read from a file on the disk.

So far, we have been using <iostream.h> header file which provide function cin / cout to take input from
console and write output to a console respectively. To understand C++ file operations like read and write,
we must first understand the concept of a stream in C++.

A stream is simply a flow of data or characters.

There are two types of streams: input streams and output streams.

An input stream is used to read the data from an external input device such as a keyboard, while an output
stream is used to write data to the external output device such as a monitor.

We can use built-in classes to access an input/output stream, i.e., “ios”.

The “iostream.h” file contains all the required standard input/output stream classes in the C++
programming language.

In addition, “ifstream,” which stands for “input file stream,” is used to read a stream of data from a file,
and “ofstream,” which stands for “output file stream,” is used to write a stream of data to a file.

To read and write from a file we are using the standard C++ library called fstream.

Note: Include <iostream> and <fstream> header files in your program to do file Operations.

1
The fstream library

The fstream library provides C++ programmers with three classes for working with files. These classes
include:

1. ofstream– This class represents an output stream. It is used for creating files and writing information
to files.

2. ifstream– This class represents an input stream. It is used for reading information from data files.

3. fstream– This class generally represents a file stream. It comes with ofstream/ifstream capabilities.
This means it is capable of creating files, writing to files, reading from data files.

File Operations

1. Creating a file: open()

2. Reading data: read()

3. Writing new data: write()

4. Closing a file: close()

Open a File

A File must be opened before we can read from it or write to it.

We can use fstream object to open a file for writing or reading purpose.

If we use ifstream object, we can open a file for reading purpose only.

If we use ofstream object, we can open a file for writing purpose only.

Following is the Standard Syntax to Open File:

fstream fileobj;

fileobj.open(“filename.txt”,ios::out);

2
File Open Modes

3
Close a File

A File must be closed after completion of all the operations related to file.

When C++ program terminates, it automatically flushes all the streams, release all the allocated memory
and closes all the opened files.

But it is always a good practice that a programmer should close all the opened files before program
termination.

Following is the standard syntax for Close the file:

fileobj.close();

Write into a File

While doing C++ Programming, we write information to a file from our program using streams insertion
Operator << just as we use that operator to output information to the screen.

The only difference is that we use an ofstream or fstream object instead of the cout object. The text to be
written to the file should be enclosed within double-quotes.

Syntax:

fileobj<<“Text here”;

Write a program to write data in a file.

4
Write a program to append data in a file.

Read from a File

Once the data is in a file, we must be able to read that data. we must open the file in a read access mode.

There are several ways to read data. We can read character data one character at a time or one string at a
time. The choice depends on the format of the data. Files we open for read access must exist already, or
C++ gives an error. we can't read a file that does not exist.

open() returns zero if the file does not exist when we open it for read access.

To read the same information form a file with space and new line, use getline(fileobj, stringvariable);

However, instead of using the cin object, you use the ifstream/ fstream object.

Syntax: while(getline(fileobj, stringvar))


{
cout << stringvar;
}

You might also like