0% found this document useful (0 votes)
4 views

File_Handling

This document provides a comprehensive overview of file handling in C++, including the use of ifstream, ofstream, and fstream classes for reading and writing files. It includes code examples for writing to files, reading from files, and a mini project for managing student records. Additionally, it outlines various file open modes and introduces an advanced project for a complete Student Management System with functionalities for adding, viewing, updating, and deleting records.

Uploaded by

Ajay Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

File_Handling

This document provides a comprehensive overview of file handling in C++, including the use of ifstream, ofstream, and fstream classes for reading and writing files. It includes code examples for writing to files, reading from files, and a mini project for managing student records. Additionally, it outlines various file open modes and introduces an advanced project for a complete Student Management System with functionalities for adding, viewing, updating, and deleting records.

Uploaded by

Ajay Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

File Handling in C++ (Complete Notes + Mini Project)

File Handling in C++

What is File Handling?

File handling in C++ refers to creating, opening, reading, writing, and closing files using standard

classes from the <fstream> header.

Classes Used:

- ifstream - Input file stream (Read data)

- ofstream - Output file stream (Write data)

- fstream - File stream (Read and write)

Include header:

#include <fstream>

File Operations

Writing to a File

#include <fstream>

using namespace std;

int main() {

ofstream file("sample.txt");

file << "Hello World\nLearning File Handling";

file.close();

return 0;
}

Reading from a File

#include <fstream>

#include <iostream>

using namespace std;

int main() {

ifstream file("sample.txt");

string content;

while (getline(file, content)) {

cout << content << endl;

file.close();

return 0;

Reading and Writing using fstream

#include <fstream>

#include <iostream>

using namespace std;

int main() {

fstream file;

file.open("sample.txt", ios::in | ios::out);


if (file.is_open()) {

file << "\nAdding another line.";

file.seekg(0);

string line;

while (getline(file, line)) {

cout << line << endl;

file.close();

else {

cout << "File could not be opened!";

return 0;

File Open Modes

Mode | Purpose

--------------|----------------------------------

ios::in | Open file for reading

ios::out | Open file for writing

ios::app | Append data at the end of file

ios::ate | Move to end immediately on open

ios::trunc | Truncate file content on open

ios::binary | Open file in binary mode


Mini Project: Student Record Management using File Handling

Program to add and display student records stored in a file

#include <iostream>

#include <fstream>

using namespace std;

class Student {

public:

string name;

int roll;

void input() {

cout << "Enter Name: ";

cin >> name;

cout << "Enter Roll No: ";

cin >> roll;

void show() {

cout << "Name: " << name << ", Roll No: " << roll << endl;

};

int main() {

Student s;
int choice;

do {

cout << "\n1. Add Record\n2. Display Records\n3. Exit\nEnter choice: ";

cin >> choice;

if (choice == 1) {

ofstream file("students.txt", ios::app);

s.input();

file << s.name << " " << s.roll << endl;

file.close();

else if (choice == 2) {

ifstream file("students.txt");

while (file >> s.name >> s.roll) {

s.show();

file.close();

} while (choice != 3);

return 0;

Important Points

- Always close files with .close().

- Check if the file opened successfully before operating on it.


- Use getline() for reading whole lines, >> for word-by-word reading.

Advanced Project: Student Management System (Add, View, Update, Delete) Using File Handling in

C++

Let's now extend our mini project into a full Student Management System with the following

functionalities:

- Add new student records

- View all student records

- Update existing student details

- Delete a student record

You might also like