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

Data_file_handling-1

Data handling the course of the bed does not currently available the course for undergraduate subjects of cell padding the time cause of the course available related posts in sql the other day and I am mo uth ke

Uploaded by

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

Data_file_handling-1

Data handling the course of the bed does not currently available the course for undergraduate subjects of cell padding the time cause of the course available related posts in sql the other day and I am mo uth ke

Uploaded by

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

Chapter-Data file handling

INTRODUCTION-KEY TERMS

 C++ provides a rich set of operations for both unformatted and formatted I/O operation. These I/O
operation are implemented through iostream library.
 A “stream” is a sequence of bytes.
 If bytes flow from a device like keyboard, disk drive etc to main memory, then it is call input
operator.
 If bytes flow from main memory to a device like display screen, printer etc, then it is called output
operator.
 A file is a bunch of bytes stored on some storage device like tape or magnetic disk etc.
 In C++ a file at its lowest level is interpreted simply as stream of bytes. File I/O library manages
the transfer of these bytes.
 At user level, it consists of a sequence of possibly intermixed data types-character, arithmetic
values and class objects.
 Fstream is the header file used for file in C++.
 Fstream consists of a set of operations for handling files related to input and output.
 The stream that supplies data to the program is known as input stream.
 The stream that receives data from the program is known as output stream.
 Fstream base is derived from the basic class ios.
 “A file is a basic unit of storage in the secondary memory.”
 A file can have related data which are identified by a ‘file name’ which is processed by the system
using the location of the file.
 The data and information generated by a program as a result of processing is stored as files in the
secondary memory.
 Programs are written to manipulate these files using a set of ‘file manipulation/handling function
defined in -> fstream, ifstream, ofstream.

Types of file stream system


 The functions of the classes and their description is given below
Classes Meanings
File buf It sets the file buffer to read and write.
Ifstream Used to read from files. Provides input operations for file. Inherits
the function get (), getline (), read() from istream.
Ofstream Used to write on files. Provides output operations for files. Inherits
the function put(), write() from ostream.
Fstream Used for both read and write from/to files. Provides support for
simultaneous I/O operations. Inherits all the function from ios
classes

Types of data files


 Files are used to store data or information permanently for future use. Two types of data files are: -
1. Text file.
2. Binary file.

1
 Text file
 In text file, each line of text is terminated with a special character known as EOF (end of file) or
determiner. It stores information in ASCII character.
 Binary file.
 A binary file, is a file that contains information in the same format in which the information is held
in memory. There is no special character (determiner) for a line. Also no translation occurs in
binary file.

Structure of files in C++


 #include<fstream.h>
Void main()
{
Ofstream myfile;
myfile.open(“text”);
myfile<<”ceba”;
myfile.close();
}

 A program involves the following data communication.


1. Data transfer between I/O units and the program.
2. Data transfer between the program and the secondary memory.

 Opening a file
 A file must be opened first before data can be read from it or written to it. There are 2 ways to open
a file with stream object.
1. Using the constructor.
2. Using open() member function.

Using constructor method


1. Input: opens the file for input purpose using the object of ifstream.
Syntax: ifstream objectname(“filename”);
Ex: ifstream infile(“abc.dat”);

2. Output: opens the file “abc.dat” for output purpose using the objects of ofstream.
Syntax: ofstream object(“filename”);
Ex: ofstream outfile(“abc.dat”);

3. Input/output: opens the file “abc.dat” for both input and output purpose using the object of
fstream.
Syntax: fstream object(“filename”);

Using open() function method


1. Input: opens the file “abc.dat” for input purpose using the function open() and the object of the
class ifstream.
Syntax: ifstream_object.open(“filename”);
Ex: in_file.open(“abc.dat”);

2
2. Output: opens the file “abc.dat” for output purpose using the function open() and the object of
the class ofstream.
Syntax: ofstream_object.open(“filename”);
Ex: out_file.open(“abc.dat”);

3. Input/output: opens the file “abc.dat” for the input and output purpose using the open() and the
object of the class fstream.
Syntax: fstream_object.open(“filename”);
Ex: in_out_file.open(“abc.dat”);

 Closing a file
 close() is used to remove the linkage between the file and stream object.
Syntax: stream_object.close();
Ex: file_out.close();

Modes of opening using fstream


Parameter Purpose
ios::app Opens the existing contents in append mode. (adds the contents at the end of
the file)
ios::in Opens a file in read only mode.
ios::out Opens a file in write only mode.
ios::ate Opens a file for updating (editing) and places the file pointer to the end of file.
ios::binary Opens a file in binary mode.
ios::trunk Opens an existing file and deletes the contents of the file.
ios::noreplace Turn down opening if the file already exists
ios:: nocreate Turn down opening id the file does not exists.

 Note: the file can be opened in multiple modes which can be given using the bitwise operator (::).

Input and output operation in text files


 put(): writes a single character to the given stream.
Syntax: ofstream_object.put(character);
Ex: char ch=’a’;
ofstream fout(“abc.txt”);
Fout.put(ch);

 get(): reads a single character from the file.


Syntax: ifstream_object.get(character);
Eg: char ch=’a’;
ifstream fin(“abc.txt”);
Fin.get(ch);

 getline(): reads number of characters represented by size.


Syntax: ifstream_object.getline(charray, size);
Eg: char name[50];
fstream file;
File.getline(name,50);

3
Input and output operation in binary files
 write(): writes binary data to a file.
Syntax: ofstream_object.write((char*)&var, sizeof(var));
Eg: student s;
ofstream_fout(‘std.dat”,ios::binary);
Fout.write((char*)&s, sizeof(s));

 read(): reads a binary data from a file.


Syntax: ofstream_object.read((char*)&var, sizeof(var));
Eg: student s;
ifstream_fin(“std.dat”,ios::binary);
Fin.read((char*)&s, sizeof(s));

End of file detection –eof()

 The EOF() function detects end of file. The function is true(non-zero) if EOF is reached,
otherwise return false.
 Ex: if(fin.eof())
{
Statements;
……
}
 Ex: while(!fin.eof())
{
Statements;
...…
}

File pointer and their manipulations


 The variables used in traversing the file opened for processing. Every file used for processing is
associated with 2 pointers.
1. Input pointer (get)pointer -> used for reading.
2. Output pointer (put)pointer -> used for writing.
3. Append mode->used to write at the end of file pointer

 Examples
1. File opened in ‘read’ mode.
C + +

Fptr

2. File opened in ‘write’ mode.

Fptr

4
3. File opened in ‘append’ mode.
C + +

Fptr
FILE POINTERS
 The file pointer can be moved to any desired location, using the following functions supported by
file system.
1. seekg(): moves get pointer to a specified location. It is given in 2 ways.
a. seekg(long);
Ex: seekg(50);
 Moves get pointer to a specified location from the beginning of the file.
b. seekg(offset, seekdir)
 Where offset is the number of bytes the get pointer is to be moved from seek dir position.
 The reference position “seekdir” can take the following values.

Pointer seek() calls


Call of seek() Action
Fout.seekg(o, ios::beg); Points to the beginning. i.e 0th place.
Fout.seekg(o, ios::cur); Points to the current location.
Fout.seekg(o, ios::end); Points to the end of the file.
Fout.seekg(m, ios::beg); Moves to(m+1)th byte on the file.
Fout.seekg(m, ios::cur); Points to mth byte from the current position in forward
direction.
Fout.seekg(-m, ios::cur); Points to mth byte from the current position in backward
direction.
Fout.seekg(-m, ios::end); Goes backward by m bytes from the end.

2. seekp(): moves put pointer to a specified location. It can be given in 2 ways.


Example: inf.seekg(20); it tells the put pointer points to 20th byte in a file from 0th byte.

3. tellp(): gives the current position of the put pointer. [gives byte number]
Example: int position= fin.tellp();

4. tellg(): gives the current position of the get pointer. [gives byte number]
Example: int position= fin.tellg();

 File processing includes the following activities.


1. Creating a file.
2. Adding and deleting a file.
3. Modifying the data.
4. Merging data from 2 files.
5. Generating reports from a file.

5
Frequently asked questions
1. Which is the header file required for handling functions in c++.
2. What is stream?
3. Name the stream used for file input/output.
4. What is input stream and output stream?
5. mention the different types of files.
6. What is the use of eof() ?
7. Differentiate between ifstream and ofstream fucntions.
8. Differentiate between read() and write().
9. What is purpose of get() and getline().
10. Explain the member functions belonging to ifstream() and ofstream().
11. Explain the different file modes in c++.

You might also like