0% found this document useful (0 votes)
6 views18 pages

Types of Files1

The document presents an overview of file operations in C++, specifically focusing on sequential and random access files. It outlines the characteristics, differences, and examples of each file type, along with sample C++ programs demonstrating file reading, writing, and copying operations. Additionally, it explains the usage of the write() and read() functions for handling objects in files.

Uploaded by

antarasartale11
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)
6 views18 pages

Types of Files1

The document presents an overview of file operations in C++, specifically focusing on sequential and random access files. It outlines the characteristics, differences, and examples of each file type, along with sample C++ programs demonstrating file reading, writing, and copying operations. Additionally, it explains the usage of the write() and read() functions for handling objects in files.

Uploaded by

antarasartale11
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/ 18

K. K. Wagh Polytechnic, Nashik.

Program: Information Technology (IF)


Semester: III Scheme: K
Course: OBJECT ORIENTED PROGRAMMING USING C++
Course Code: 313304
Unit-5: File Operations
Topic:5.4 Types of Files

Presented by:
Mrs. A. A. Shaikh
Lecturer in Information
Technology
Contents :

• Sequential Access File.

• Random Access File.

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Types of Files:

Types of Files

Sequential File Random Access File

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Sequential File :

• In a sequential access file, data is accessed in a linear,


sequential manner, from start to end.

• Each read or write operation must follow the previous one.

• If we want to access data in this type , we have to traverse


from starting data to the data we want to access .

• Data / Contents / Information are stored and access in a


sequential format .

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Random Access File :

• In Random access , We can access data /


contents / information stored in a file
randomly stored anywhere in the file easily .

• If we want to access data in this type , we


can directly access that particular data (no
need to traverse from start ).

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Difference Between Sequential Access File & Random Access File :

Sr. No Sequential Access File Random Access File


1. Data is stored and read sequentially Data is read or access randomly
2. To read a record , it is required to read To read a record , directly it can be read
all records sequentially before it . .
3. More Access time Less Access time.
4. Sequential access files store records in a Random access files do not have any
specific order, usually the order in which specific order of storing records.
they were added to the file.

5. Inserting a new record in a sequential Random access files may require


access file is relatively easy since new relocating other records to maintain the
records are added to the end of the file. order so insertion becomes hard as
compared to sequential access.

6. Sequential access files are typically Random access files are typically
organized in a linear fashion indexed.
7. Example - Text files Example- Database, Spreadsheet

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Implement program using sequential input and output operations on file

#include<iostream.h> for(int i=0; i<len;i++)


#include<conio.h> file.put(string[i]);
#include<fstream.h> file.seekg(0);
#include<string.h> char ch;
while(!file.eof())
void main() {
{ file.get(ch);
char string[60]; cout<<ch;
cout<<"Enter a string:"; }
cin>>string; file.close();
int len=strlen(string); }
fstream file;
file.open(“Stud.txt",ios::in|ios::out);

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Program to count number of lines in a file :

include<iostream.h> while(in)
#include<conio.h> {
#include<fstream.h> in.get(ch);
if(ch=='\n')
void main() n++;
{ }
clrscr(); cout<<"Number of lines in a file
ifstream in; are:"<<n;
char ch; in.close();
int n=0; getch();
in.open("stud.txt"); }

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Reading from a File
#include <iostream.h> while (!st.eof())
#include<conio.h>
#include <fstream.h> {
int main() st >>ch; // Step 4: Reading from
{ file
fstream st; // step 1: Creating object of fstream
class
st.open("stud.txt",ios::in); // Step 2: Creating new cout << ch; // Message Read from
file file
if(!st) // Step 3: Checking whether file exist
{ }
cout<<"No such file"; st.close(); // Step 5: Closing file
} }
else
{ getch();
char ch; return 0;
}

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Writing to a File
#include <iostream.h>
else
#include<conio.h>
#include <fstream.h> {
int main() cout<<"New file created";
{ st<<"Hello"; // Step 4: Writing to
fstream st; // Step 1: Creating file
object of fstream class st.close(); // Step 5: Closing file
st.open("study.txt",ios::out); // Step 2:
}
Creating new file
if(!st) // Step 3: Checking whether file getch();
exist return 0;
{ }
cout<<"File creation failed";
}

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Program to read and write in a file at a same time

#include<iostream.h>
if(!st)
#include<conio.h>
{
#include<fstream.h>
cout<<"error in reading
int main()
file";
{
}
fstream st;
else
clrscr();
{
st.open("sample.txt",ios::out);
char ch;
if(!st)
while(!st.eof())
{
{
cout<<"file creation failed";
st>>ch;
}
cout<<ch;
cout<<"file created
}
successfully"<<endl;
st.close();
st<<"hello";
}
st.close();
getch();
st.open("sample.txt",ios::in);
return 0;
}

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


C++ 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.
• Syntax of write() function

• fstream fout;
• fout.write( (char *) &obj, sizeof(obj) );
• Where
&obj : Initial byte of an object stored in
memory.
sizeof(obj) : size of object represents the total
number of bytes to be written
from initial byte.
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Program :

#include<fstream.h> cin>>marks; void main()


#include<conio.h> } {
class Student public: Student S;
{ void AddRecord() char ch='n';
int roll; { do
char name[25]; fstream f; {
float marks; Student Stu; S.AddRecord();
void getdata() f.open("Student.dat",ios::app|ios: cout<<"\n\nDo you want to add
{ :binary); another data (y/n) : ";
cout<<"\n\nEnter Roll : "; Stu.getdata(); ch = getche();
cin>>roll; f.write( (char *) &Stu, sizeof(Stu) } while(ch=='y' || ch=='Y');
); cout<<"\nData written
cout<<"\nEnter Name : "; f.close(); successfully...";
cin>>name; } }
}; getch();
cout<<"\nEnter Marks : "; }

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


C++ read() function

• The read() function is used to read object (sequence of bytes)


to the file.

• Syntax of read() function

fstream fin;
fin.read( (char *) &obj, sizeof(obj) );
• The read() function takes two arguments.
• &obj : Initial byte of an object stored in file.
• sizeof(obj) : size of object represents the total number of
bytes to be read from initial byte.
• The read() function returns NULL if no data read.

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Program :

#include<fstream.h> f.open("Student.dat",ios::in|ios::binary);
#include<conio.h> cout<<"\n\tRoll\tName\tMarks\n";
class Student while( (f.read((char*)&Stu,sizeof(Stu))) !=
{ NULL )
int roll; Stu.putdata();
char name[25]; f.close();
float marks; }
void putdata() };
{
cout<<"\n\t"<<roll<<"\t"<<name<<"\t"<<mar void main()
ks; {
} Student S;
public: S.Display();
void Display() }
{
fstream f;
Student Stu;

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Copy the contents of one file to other file

#include<iostream.h> if(!fs) while(fs.eof()==0)


#include<conio.h> { {
#include<fstream.h> cout<<"Error in fs>>ch;
#include<stdio.h> opening source file"; ft<<ch;
getch(); }
void main() } cout<<"File copied
{ successfully";
clrscr(); cout<<"Enter target file fs.close();
ifstream fs; name:"; ft.close();
ofstream ft; gets(fname2); getch();
ft.open(fname2); }
char ch, fname1[20],fname2[20];
cout<<"Enter source file if(!ft)
name:"<<endl; {
gets(fname1); cout<<"Error in
fs.open(fname1); opening target file:";
fs.close();
getch();
}

Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik


Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik

You might also like