0% found this document useful (0 votes)
12 views22 pages

4.2 Disk - IO - File - Stream

Uploaded by

hetavimodi2005
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)
12 views22 pages

4.2 Disk - IO - File - Stream

Uploaded by

hetavimodi2005
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/ 22

Disk File I/O with Streams

--Prof. S.N.Shelke
Disk File I/O with Streams
 Using a file in a program is a simple three-step process

 The file must be opened.

 If the file does not yet exits, opening it means creating it.

 Use File

 Read Content from the file

 Copy content to File

 When the program is finished using the file, the file must be closed.
File streams

 File Stream are

 Binary file

 Text File
File streams

 Binary File:-

 The stream of this type is set by applying ios::binary flag.

 It is set while opening a file.

 Text Stream:

 If ios::binary is not set treated as a text stream.


Setting program for File I/O

 Before file I/O can be performed, a C++ program must be set up properly.

 File access requires the inclusion of fstream.h


Opening a File

 Before data can be written to or read from a file, the file must be opened.

open (filename, mode);


File
 Let see fstream which defines three new data types:

 To perform file processing in C++, header files <iostream> and <fstream> must be

included in your C++ source file.


File Opening Modes
Mode Explanation

ios :: in Open a file for reading

ios :: out Open a file for writing

ios :: app Appends data to the end of the file

File pointer moves to the end of the file but


ios :: ate
allows to writes data in any location in the file

ios :: binary Binary File

ios :: trunc Deletes the contents of the file before opening


All these flags can be combined using the bitwise operator OR (|).

 For example

ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
Default Mode for File opening

class default mode parameter


ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
Closing File

 Once file operation are completed it must me notified to system using

stream member function close.

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

You might also like