Lab 12 Filing - 2
Lab 12 Filing - 2
File I/O
NIDA MUNAWAR
Introduction to filing
Files are used to store data in a storage device permanently.
File handling provides a mechanism to store the output of a program in a
file and to perform various operations on it.
Streams
C++ I/O occurs in streams, which are sequences of bytes.
In input operations, the bytes flow from a device (e.g., a keyboard, a disk drive, a
network connection, etc.) to main memory.
In output operations, bytes flow from main memory to a device (e.g., a display
screen, a printer, a disk drive, a network connection, etc.).
So far, we have been using the iostream standard library, which
provides cin and cout methods for reading from standard input and writing to
standard output respectively.
However, as a programmer, you should learn to close open files before the
program terminates.
The fstream, ofstream, and ifstream objects have the close() function for closing
files. The function takes this syntax:
void close();
Parameters:
• is: It is an object of istream class and tells the function about the
stream from where to read the input from.
• str: It is a string object, the input is stored in this object after being
read from the stream.
• delim: It is the delimitation character which tells the function to stop
reading further input after reaching this character.
Syntax:
istream& getline (istream& is, string& str);
The second declaration is almost the same as that of the first one. The only
difference is, the latter have a delimitation character which is by default new
line(\n) character.
Read a line
Read character by character
Example
Examples make use of additional functions from cin object, like getline() function
to read the line from outside and ignore() function to ignore the extra characters
left by previous read statement
write() function
The write() function is used to write object or record (sequence of bytes) to the file. A
record may be an array, structure or class.
fstream fout;
fout.write( (char *) &obj, sizeof(obj) );
#include <iostream>
#include <fstream>
using namespace std;
class student{
int roll;
char name[25];
float marks;
public:
void getdata(){
cout << "enter roll no" << endl;
cin >> roll;
void addRecord()
{
fstream f;
student s;
f.open("studen.dat",ios::app | ios::binary );
s.getdata();
f.write((char*)&s, sizeof(s));
f.close();
}
};
int main(){
student s;
char c = 'n';
do{
s.addRecord();
cout << "do you want to add another record";
cin >> c;
} while (c == 'y' || c == 'Y');
cout << "data written successfully";
read() function
The read() function is used to read object (sequence of bytes) to the file.
fstream fin;
fin.read( (char *) &obj, sizeof(obj) );
#include <fstream>
class student{
int roll;
char name[25];
float marks;
public:
void displayStudent(){
<<"NAME: "<<name<<endl
<<"Marks: "<<marks<<endl;
void Readdata()
fstream f;
student s;
f.open("studen.dat",ios::in | ios::binary);
if(f.read((char*)&s,sizeof(s))){
cout<<endl<<endl;
s.displayStudent();
else{
};
int main(){
student s;
s.Readdata();
return 0;
Exercise
QUESTION#1
Write a program to implement I/O operations on characters. I/O operations includes inputting a
string,
calculating length of the string, Storing the String in a file and fetch the stored characters from it.
QUESTION#2
Write a program to copy the contents of one file to another.
QUESTION#3
Take a class Person having two attributes name and age.
Include a parametrized constructor to give values to all data members.
In main function
QUESTION#4
Take a class Participant having three attributes (ID, name and score) and following member
functions
Input () function takes data of the object and stores it in a file name participant.dat
Output () function takes id from user and show respective data of that id.
Max () gives the highest score of the Participant in the file.
QUESTION#6
Write a function in C++ to count and display the number of lines not starting with alphabet 'A' present in
a text file "STORY.TXT". Example:
1. If the file "STORY.TXT" contains the following lines,
2. The rose is red.
3. A girl is playing there.
4. There is a playground.
5. An aeroplane is in the sky.
6. Numbers are not allowed in the password.
7. The function should display the output as 3.