
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
File Handling through C++ Classes
In this tutorial, we will be discussing a program to understand File Handling through C++ classes.
The default functions used in file handling to interact with files can be defined by the user using classes. Below is the implementation of ifstream and ofstream functions.
Example
#include <iostream> #include <fstream> using namespace std; int main(){ //creating ofstream object ofstream fout; string line; fout.open("sample.txt"); //initiating loop if file is opened while (fout) { getline(cin, line); if (line == "-1") break; fout << line << endl; } fout.close(); ifstream fin; fin.open("sample.txt"); while (fin) { getline(fin, line); cout << line << endl; } fin.close(); return 0; }
- Related Articles
- File Handling in C#
- tellp() in file handling with C++
- Basics of File Handling in C
- Basics of File Handling in C Programming
- File Handling in Java using FileReader and FileWriter
- Set position with seekg() in C++ language file handling
- MySQL Command-Line Options that Affect Option-File Handling
- How many Python classes should I put in one file?
- Download file through an AJAX call in PHP
- How to configure handling and formatting of log file in Selenium with python?
- Would you recommend to define multiple Python classes in a single file?
- Looping through the content of a file in Bash
- Exposing xsjs file data through web browser in SAP HANA
- Running the collection using Newman through JSON file in Postman
- Pseudo-classes and CSS Classes

Advertisements