4.2 Disk - IO - File - Stream
4.2 Disk - IO - File - Stream
--Prof. S.N.Shelke
Disk File I/O with Streams
Using a file in a program is a simple three-step process
If the file does not yet exits, opening it means creating it.
Use File
When the program is finished using the file, the file must be closed.
File streams
Binary file
Text File
File streams
Binary File:-
Text Stream:
Before file I/O can be performed, a C++ program must be set up properly.
Before data can be written to or read from a file, the file must be opened.
To perform file processing in C++, header files <iostream> and <fstream> must be
For example
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
Default Mode for File opening
fileObject.close();
Open file for Writing
#include<iostream> Output:
#include<fstream>
Data written to file
using namespace std;
int main()
{
ofstream ofile;
Contains of file.txt:
ofile.open("file.txt");
ofile << "This is a line in a file" << endl; This is a line in a file
ofile << "This is another line" << endl; This is another line
cout << "Data written to file" << endl;
ofile.close(); // close the file
return 0;
}
Open file for Reading
#include<iostream>
Output:
#include<fstream>
using namespace std; Reading data from a file :-
int main() {
char data[100]; This is a line in a file
ifstream ifile; This is another line
ifile.open("file.txt");
cout << "Reading data from a file :-" << endl << endl;
while (!ifile.eof()) {
ifile.getline(data, 100); // read a line from file
Contains of file.txt:
cout << data << endl; // print the file to console
} This is a line in a file
This is another line
ifile.close(); // close the file
return 0;
}
File Operations using fstream: Write
#include<iostream> else {
#include<fstream> // proceed with further operations
using namespace std; cout << "Enter a line : ";
cin.getline(line, 100);
int main() { file << line << endl; // Append the line to the file
char line[100]; cout << "Line written into the file" << endl;
fstream file; // declare an object of fstream }
class file.close();
file.open("file.txt", ios :: out | ios :: app); // return 0;
open file in append mode }
if (file.fail()) { // check if file is opened
//successfully
// file opening failed
cout << "Error Opening file ... " << endl;
}
File Operations using fstream: Read
#include<iostream>
else {
#include<fstream>
while (!file.eof()) {
using namespace std;
file.getline(line, 100);
int main() {
cout << line << endl;
char line[100];
}
fstream file; // declare an object of fstream
file.close();
class
return 0;
file.open("file.txt", ios :: in);
}
if (file.fail()) { // check if file is opened
successfully
// file opening failed
cout << "Error Opening file ... " << endl;
}
Reading /Writing class objects to a file
#include<iostream> void student :: getdata() {
#include<fstream> cout << "\nEnter Roll No. : ";
using namespace std; cin >> rollno;
class student cin.ignore();
{ cout << "Enter Name : ";
int rollno; cin.getline(name, 30);
char name[30]; cout << "Enter Marks : ";
float marks; cin >> marks;
public: }
student() { } void student :: display() {
void getdata(); cout << "\nRoll No. : " << rollno << endl;
void display(); cout << "Name : " << name << endl;
}; cout << "Marks : " << marks << endl;
}
int main() { file.open("objects.txt", ios :: in);
student s[3]; cout << "\nReading Student
fstream file; information from file ::::" << endl;
int i; for (i = 0; i < 3; i++)
file.open("objects.txt", ios :: out); {
cout << "\nWriting Student information file.read((char *)&s[i], sizeof(s[i]));
to the file :::: " << endl; s[i].display();
for (i = 0; i < 3; i++) }
{ file.close(); // close the file
s[i].getdata();
file.write((char *)&s[i], sizeof(s[i])); return 0;
} }
file.close(); // close the file
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer
Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune