Object Oriented Programming: Chapter 6: IO Systems and File IO

File Modes

C++ Object Oriented Programming

The file operations are associated with the object of one of the classes: ifstream, ofstream or fstream. Hence we need to create an object of the corresponding class.

File Modes

• The file operations are associated with the object of one of the classes: ifstream, ofstream or fstream. Hence we need to create an object of the corresponding class.

• The file can be opened by the function called open(). The syntax of file open is ‒

Open(filename,mode)

• The filename is a null terminated string that represents the name of the file that is to be opened. The mode is optional parameter and is used with the flags as given below ‒

ios::in         ‒  Open for input operation.

ios::out       ‒  Open for output operation.

ios::binary  ‒  Open for binary operations.

ios::ate      ‒    If this flag is set then initial position is set at the end of the file otherwise initial position is at the beginning of the file.

ios::app      ‒    The output operations are appended to the file. This is an appending mode. That means contents are inserted at the end of the file.

ios::trunc     ‒   The contents of pre‒existing file get destroyed and it is replaced by new one.

•  We can use open() function using the above given syntax as ‒

ofstream obj;

obj.open("sample.bin",ios::out | ios::binary)

That means the file sample.bin is opened for output operation in binary mode. Thus we can combine the flags using OR operator( | ).

•  The is_open() is a boolean function that can be used to check whether the file is For open or not.

For example

if(obj.is_open())

{

      cout<<"File is Successfully opened for operations";

}

 

Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - File Modes


Object Oriented Programming: Chapter 6: IO Systems and File IO



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation