cheat code to ace OOPS lab, Cheat Sheet of Programming Languages

cheat sheet to get success in lab exam

Typology: Cheat Sheet

2023/2024

Uploaded on 07/12/2025

asiya-munir
asiya-munir 🇸🇬

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Important File Handling Functions
File Handling:
In C++, file handling allows programs to store data in files and perform various operations on
them. Files can be used to save program output or retrieve data for processing.
A stream is a concept that represents a source or destination for data, like a file or a console. In
C++, there are three main classes for file handling:
1. ofstream: Used to create and write data to a file.
2. ifstream: Used to read data from a file.
3. fstream: Can both read from and write to a file.
These classes are part of the fstream library, so you need to include this header when working
with files in C++.
1. Opening a File (open() function)
The open() function is used to open a file in different modes (read, write, append, etc.).
Syntax:
fstream file;
file.open("example.txt", ios::out); // Opens file for writing
2. Writing to a File (write operation)
The << operator is used to write data into a file.
Example:
fstream file;
file.open("example.txt", ios::out);
file << "Hello, File Handling!"; // Writing to file
file.close(); // Always close the file after use
3. Reading from a File (read operation)
The >> operator is used to read data from a file.
pf2

Partial preview of the text

Download cheat code to ace OOPS lab and more Cheat Sheet Programming Languages in PDF only on Docsity!

Important File Handling Functions

File Handling:

In C++, file handling allows programs to store data in files and perform various operations on them. Files can be used to save program output or retrieve data for processing. A stream is a concept that represents a source or destination for data, like a file or a console. In C++, there are three main classes for file handling:

  1. ofstream : Used to create and write data to a file.
  2. ifstream : Used to read data from a file.
  3. fstream : Can both read from and write to a file. These classes are part of the fstream library, so you need to include this header when working with files in C++.

1. Opening a File (open() function)

The open() function is used to open a file in different modes (read, write, append, etc.).

Syntax:

fstream file; file.open("example.txt", ios::out); // Opens file for writing

2. Writing to a File (write operation)

The << operator is used to write data into a file.

Example:

fstream file; file.open("example.txt", ios::out); file << "Hello, File Handling!"; // Writing to file file.close(); // Always close the file after use

3. Reading from a File (read operation)

The >> operator is used to read data from a file.

Example:

fstream file; file.open("example.txt", ios::in); char ch; while (file >> ch) { cout << ch; // Displaying file content } file.close();

4. Closing a File (close() function) It is important to close the file after performing operations using the close() function.

Syntax:

file.close();