Types of Files1
Types of Files1
Presented by:
Mrs. A. A. Shaikh
Lecturer in Information
Technology
Contents :
Types of Files
6. Sequential access files are typically Random access files are typically
organized in a linear fashion indexed.
7. Example - Text files Example- Database, Spreadsheet
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"); }
#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;
}
• 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 :
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.
#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;