Chapter 4. File Operations
Chapter 4. File Operations
Chapter 4
Mekonnen K.
(MSc) Email: mekonnen307@gmail.com
Topics
2 Topics Subtopics
4: 4.1. Introduction
File 4.2. Stream classes
Operation 4.3. Writing and reading modes
(File I/O) 4.4. Writing to and reading from files
4.5. Types of files (Text and Binary)
4.6. File access methods (sequential and random-
access files)
4.7. Object Oriented Programming Concept
4.7.1 Programming Paradigm
4.7.2. Overview of programming principal
4.7.3. Class and Object
4.7.4. Pillars of Object-Oriented Programming
Concept
4.7.4.1. Encapsulation,
4.7.4.2. Abstraction,
4.7.4.3. Inheritance
4.7.4.5. Polymorphism
File
o A file is a collection of related data stored in a
particular area on the disk .
o The data is stored in disk using the concept of file .
o A computer file is stored on a secondary storage
device (e.g., disk);
is permanent;
can be used to
◼ provide input data to a program
◼ or receive output data from a program
◼ or both;
should reside in Project directory for easy access;
must be opened before it is used.
Why File
input_stream.open("numbers.txt“)
File Name
Calling Dot
Dir:\\folder\fileName.extention
Object Operator
Extention ( .dat, .out, .txt)
Open and close a file
eg:-
ofstream outfile; // create stream
outfile . open (“DATA1”); // connect stream to DATA1
……………………………..
……………………………..
outfile . Close(); //disconnect stream from
DATA1
outfile . Open(“DATA2”); //connect stream to DATA2
……………………………..
……………………………..
outfile . close();
……………………………..
Validate the file before trying to access
If ( ! Mystream) If ( ! Mystream.is_open())
{ {
Cout << “File is not open.\n ”;
Cout << “Cannot open file.\n ”;
} }
File I/O Example: Open the file with validation
First Method (use the constructor) Second Method ( use Open function)
#include <fstream> #include <fstream>
using namespace std;
using namespace std;
int main()
int main()
{
{
//declare output file variable
//declare and automatically ofstream outFile;
open the file
// open an exist file fout.txt
ofstream outFile(“fout.txt");
outFile.open(“fout.txt”);
// Open validation // Open validation
if(! outFile) { if(! outFile.is_open() ) {
Cout << “Cannot open file.\n ”; Cout << “Cannot open file.\n ”;
return 1; return 1;
} }
return 0; return 0;
} }
Mode of file opening
o Both ios :: app and ios :: ate take us to the end of the file
when it is opened. The difference between the two
parameters is that the ios :: app allows us to add data to
the end of file only, while ios :: ate mode permits us to add
data or to modify the existing data any where in the file.
Mode of file opening cont’d…
eg :-
fstream file;
#include <fstream>
int main(void)
{
ofstream outFile("file1.txt", ios::out);
outFile << "That's new!\n";
outFile.close();
Return 0;
}
If you want to set more than one open mode, just use the OR
operator- |. This way:
ios::in | ios::out
Writing to a File
Example: fin.seekg(10,ios::beg);
Example: fout.seekp(10,ios::beg);
Syntax: file_pointer.tellp();
• Text files
• Binary files
Create an Object
}; myCar.model;
return 0;}
C++ Polymorphism