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

Binary and ASCII Files

C++ Object Oriented Programming

1. Handling Binary Files 2. Handling ASCII Files

Binary and ASCII Files


1. Handling Binary Files

•  We can make use of two functions namely: read and write for handling the binary file format.

• The syntax of read and write functions will be ‒

output_obj. write ((char*) & variable, size of (variable));


• The memory block must be typecast to pointer to character type This block acts as a buffer in which read contents can stored or the data to be written in the file can be stored.

• Following is a simple program in which we have some data in array a. We will be writing the contents of array a to the file "input.dat". Then we will read the file and retrieve the contents in another array b. Finally the array b will be displayed on the console.

C++ Program

#include<iostream>

#include<fstream>

#include<cstdlib>

void main()

{

ofstream out_obj;

int a[5]={10,20,30,40,50};

int b[5];

out_obj.open("input.dat");

out_obj.write((char *)&a,sizeof(a));

out_obj.close();

ifstream in obj;

in_obj.open("input.dat");

in_obj.read((char *)&b,sizeof(b));

cout<<"\n The contents of input.dat file are...\n";

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

cout<<"\n"<<b[i];

{

       in_obj.close();

}

Output

The contents of input.dat file are...

10

20

30

40

50


2. Handling ASCII Files

• An ASCII file is a plain text file whose bytes are in the ASCII range (0‒127). In practice most plain text files are UTF‒8, but the same C++ text APIs work for ASCII.

• Use <fstream>: std::ofstream for writing, std::ifstream for reading, std::fstream for read+write.

Refer previous section for the reading and writing to the file.

 

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


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