Home | | Object Oriented Programming and Data Structures | Various file modes and its syntax

Chapter: Object Oriented Programming and Data Structure : Inheritance and Polymorphism

Various file modes and its syntax

To handle large volumes of data, we need to use some devices such as floppy disk or hard disk to store the data. The data is stored in these devices using the concept of files.

The various file modes and its syntax

 

FILE

 

To handle large volumes of data, we need to use some devices such as floppy disk or hard disk to store the data. The data is stored in these devices using the concept of files. A file is a collection of related data stores in a particular area on the disk. Programs can be designed to perform the read and write operations on these files.

 

A program involves either or both of the following kinds of data communication:

1. Data transfer between the console unit and the program.

2. Data transfer between the program and a disk file.

 

The I/O system of C++ uses file stream as an interface b/n the programs and the files. 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. In other words, the input stream reads data from the file and the output stream writes data to the file.

 

Classes for File Stream Operations:

 

The I/O system of C++ contains a set of classes that define the file handling methods. These include ifstream, ofstream and fstream. These classes are contained in the header file fstream.

 

This file should be included for performing file operations.

 

The filename is a string of characters that make up a valid filename for the operating system. It may contain two parts, a primary name and an optional period with extension.

 

Examples:

Input.txt

Student

 

For opening a file, we must create a file stream and then link it to the filename. There are two ways of opening a file:

 

·        Using the constructor function of the class.

Using the member function open() of the class.

The first method is useful when only one file in the stream is used. The second method is useful when multiple files are managed using one stream.

 

File Modes:

 

The two methods that we discussed can also take two arguments instead of one. The second argument will specify the file-mode. The general form of the function open() with two arguments is:

 

stream-object.open (“filename”, mode);

 

Parameter Meaning

 

ios::app Append to end of file ios::ate Go to end of file on opening ios::binary Binary file

 

ios::in Open file for reading only

 

ios::nocreate Open fails if the file does not exist ios::noreplace Open fails if the file already exists ios::out Open file for writing only

 

ios::trunc Delete the contents of the file if it exists File pointers and their manipulations:

 

Each file has two associated pointers known as the file pointers. They are input pointer and output pointer. The input pointer is used for reading the contents of a given file location and the output pointer is used for writing to a given file location.

 

Read only mode:

Input pointer is automatically set at the beginning so that we can read the file from start.

 

Write only mode:

Existing contents are deleted and the output pointer is set at the beginning.

 

 

Append mode:

 

The output pointer is set to the end of file. Functions for manipulations of file pointers:

 

seekg() – Moves input pointer to a specified location seekp() – Moves output pointer to a specified location tellg() – Gives the current position of the input pointer tellp() – Gives the current position of the output pointer Example:

infile.seekg(10);

 

moves file pointer to the byte number 10. The bytes are numbered from zero hence it points to the 11th byte in the file.

 

ofstream out out.open(“filename”,ios::app);

int p = out.tellp();

 

The above program will give the number of bytes in the file, since the file is opened in the append mode.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Inheritance and Polymorphism : Various file modes and its syntax |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.