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

Lecture 42 - File Handling

Uploaded by

Nabia Faisal
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)
16 views

Lecture 42 - File Handling

Uploaded by

Nabia Faisal
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/ 11

SCHOOL OF ELECTRICL ENGINEERING &

COMPUTER SCIENCE (SEECS)

File Handling
Dr. Arham Muslim
Last Lecture - Recap
• Functions and Pointers
• Passing By Pointer vs Passing Variable By Reference

• Pointers to Functions

• C++ Program Memory Management


• Dynamic Memory Allocation

• Structures and Pointers

File Handling 2
Today’s Agenda
• File Handling in C++

• File Handling Libraries


• Base Libraries

• Working with File Handling Libraries


• Opening a File
• Reading a File
• Creating and Writing to a File

File Handling 3
File Handling in C++
• Data storage and retrieval from computer hard disk during the
execution of applications

• Process:
• Naming a file

• Opening a file

• Writing data into the file

• Reading data from the file

• Closing a file
File Handling 4
File Handling Libraries
• Required Header Files
• The fstream library allows us to work with files
• To use the fstream library, the following header files are required
#include <iostream>
#include <fstream>

• There are three classes included in the fstream library


• ifstream: Reads from files

• ofstream: Creates and writes to files

• fstream: Combination of both previous classes: create, read, and write


to files
File Handling 5
Base Libraries

File Handling 6
Opening a File
• The first step is to open a file for reading or writing operation
• We can open a file by:
• Passing the file name in the constructor at the time of object creation
• Using the open method

• Open a file using the constructor


• ifstream (const char* filename, openmode mode = ios::in);
• E.g. ifstream fin ("demo.txt");

• Open a file using the open method


• Calling the default constructor: ifstream fin;
• E.g. fin.open("demo.txt", ios::in);

File Handling 7
Reading a File – Example
Code Output
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string>
#include <string> using namespace std;
using namespace std;
int main() {
int main() { string myText;
ifstream MyReadFile("read_demo.txt");
string myText;
ifstream MyReadFile("read_demo.txt"); if (!MyReadFile.is_open()) {
cerr << "Error: Unable to open file." << endl;
return 1;
if (!MyReadFile.is_open()) { }
cout << "Error: Unable to open file." << endl;
return 1; while (getline(MyReadFile, myText))
} cout << myText << endl;

MyReadFile.close();
while (getline(MyReadFile, myText)) return 0;
cout << myText << endl; }

MyReadFile.close();
return 0;
} File Handling 8
Modes
Member Constant Stands For Access
File open for reading: the internal stream buffer supports
ios::in input
input operations.

File open for writing: the internal stream buffer supports


ios::out output
output operations.
ios::binary binary Operations are performed in binary mode rather than text.
ios::ate at end The output position starts at the end of the file.
All output operations happen at the end of the file, appending
ios::app append
to its existing contents.

Any contents that existed in the file before it is open are


ios::trunc truncate
discarded.
ios::nocreate Do not create Does not allow to create new file if it does not exist.
ios::noreplace Do not replace Does not replace old file with new file.
File Handling
Creating and Writing To a File – Example
Code Output to a File
#include <iostream> This is a demo text written to a file.
#include <fstream> A new line!
using namespace std;

int main() {
// Create and open a text file
ofstream MyFile("demo.txt");

// Write to the file


MyFile << "This is a demo text written to a file.";
MyFile << "\nA new line!";

// Close the file


MyFile.close();
}

File Handling 10
Additional Resources
• File Handling through C++ Classes - GeeksforGeeks

• Programming and Problem Solving with C++, 7th Edition


• Chapter 4: Program Input and the Software Design Process
• Section 4.4 File Input and Output
• Section 4.5 Input Failure

File Handling 11

You might also like