//program to write string to the file
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
void main()
{ char text[100];
clrscr();
ofstream ofile("firoz.txt",ios::app);
cout<<"enter text( press # for exit)\n";
cin.getline(text,100,'#');
ofile<<text;
getch();
}
//program to display the content by line by line
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{ char text[100];
clrscr();
ifstream ifile("firoz.txt");
if(!ifile)
{ cout<<"cannot open";
getch();
exit(0);
}
while(!ifile.eof())
{ ifile.getline(text,100);
cout<<text<<"\n";
}
getch();
}
//program to display the content char by char
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{ clrscr();
char t;
ifstream ifile("firoz.txt");
if(!ifile)
{ cout<<"can not open";
getch();
exit(0);
}
while(ifile)
{ ifile.get(t); cout<<t;
}
getch();
}
//program to display the content by word by word
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{ char text[100];
clrscr();
ifstream ifile("firoz.txt");
if(!ifile)
{ cout<<"cannot open";
getch();
exit(0);
}
while(!ifile.eof())
{ ifile>>text;
cout<<text<<"\t";
}
getch();
}
//for removing a file
#include<stdio.h>
#include<conio.h>
void main()
{ remove("firoz.txt");
getch();
}
//for renaming a file
#include<stdio.h>
#include<conio.h>
void main()
{ rename("firoz.txt","roja.txt");
getch();
}
//data file handiling using class
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class student
{ char name[20];
int reg;
int mark;
public:
void read();
void disp();
int retreg()
{ return reg;
}
}s;
void student::read()
{
cout<<"enter name";
gets(name);
cout<<"enter regno";
cin>>reg;
cout<<"enter mark";
cin>>mark;
}
void student::disp()
{
cout<<"\nname:"<<name;
cout<<"\nregno:"<<reg;
cout<<"\nmark:"<<mark;
}
void main()
{
clrscr();
int z,sreg;char se;
cout<<"\n\t\tmenu";
cout<<"\n\t\t1. read";
cout<<"\n\t\t2. disp";
cout<<"\n\t\t3. search by reg";
do{
cout<<"\n enter UR selection";
cin>>z;
switch(z)
{ case 1:
ofstream ifile("stud.dat",ios::app|ios::binary);
if(!ifile)
{
cout<<"cannot open";
getch();
exit(0);
}
cout<<"\nenter details of student\n";
s.read();
ifile.write((char*)&s,sizeof(s));
ifile.close();
break;
case 2:
cout<<"all details\n";
ifstream ofile("stud.dat",ios::binary|ios::out);
ofile.read((char*)&s,sizeof(s));
while(!ofile.eof())
{ s.disp();
ofile.read((char*)&s,sizeof(s));
} ofile.close();
break;
case 3:
cout<<"enter rg no";
cin>>sreg;
ifstream sfile("stud.dat",ios::binary|ios::out);
sfile.read((char*)&s,sizeof(s));
while(!sfile.eof())
{
if(sreg==s.retreg())
s.disp();
sfile.read((char*)&s,sizeof(s));
} sfile.close();
break;
}
cout<<"do U want to continue";
cin>>se;
}while(se=='y');
getch();
}
//data file handiling using structure
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{ char name[20];
int reg;
int mark;
}s;
void main()
{ clrscr();
int z,sreg;char se;
cout<<"\n\t\tmenu";
cout<<"\n\t\t1. read";
cout<<"\n\t\t2. disp";
cout<<"\n\t\t3. search by reg";
do{
cout<<"\n enter UR selection";
cin>>z;
switch(z)
{ case 1:
ofstream ifile("stud.dat",ios::app|ios::binary);
if(!ifile)
{cout<<"cannot open";
getch();
exit(0);
}
cout<<"\nenter details of student\n";
cout<<"enter name";
gets(s.name);
cout<<"enter regno";
cin>>s.reg;
cout<<"enter mark";
cin>>s.mark;
ifile.write((char*)&s,sizeof(s));
ifile.close();
break;
case 2:
cout<<"all details\n";
ifstream ofile("stud.dat",ios::binary|ios::out);
ofile.read((char*)&s,sizeof(s));
while(!ofile.eof())
{ cout<<"\nname:"<<s.name;
cout<<"\nregno:"<<s.reg;
cout<<"\nmark:"<<s.mark;
ofile.read((char*)&s,sizeof(s));
} ofile.close();
break;
case 3:
cout<<"enter rg no";
cin>>sreg;
ifstream sfile("stud.dat",ios::binary|ios::out);
sfile.read((char*)&s,sizeof(s));
while(!sfile.eof())
{
if(sreg==s.reg)
{ cout<<"\nname:"<<s.name;
cout<<"\nregno:"<<s.reg;
cout<<"\nmark:"<<s.mark;}
sfile.read((char*)&s,sizeof(s));
} sfile.close();
break;
}
cout<<"do U want to continue";
cin>>se;
}while(se=='y');
getch();
}
//file handiling with edit & delete
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
class student
{ char name[20];
int reg, mark;
public:
void read();
void disp();
void edit();
int retreg()
{ return reg;
}
}s;
void student::read()
{ cout<<"enter name";
gets(name);
cout<<"enter regno";
cin>>reg;
cout<<"enter mark";
cin>>mark;
}
void student::disp()
{ cout<<"\nname:"<<name;
cout<<"\nregno:"<<reg;
cout<<"\nmark:"<<mark;
}
void student::edit()
{ char nname[20];
int nreg,nmark;
cout<<"\nenter new name(if no change pressenter key)";
gets(nname);
cout<<"\nenter new reg no(if no change press -1)";
cin>>nreg;
cout<<"\nenter new mark no(if no change press -1)";
cin>>nmark;
if (strlen(nname)!=0)
strcpy(name,nname);
if(nreg!=-1)
reg=nreg;
if(nmark!=-1)
mark=nmark;
}
void main()
{
clrscr();
int z,sreg;char se;
do{
cout<<"\n\t\tmenu";
cout<<"\n\t\t1. read";
cout<<"\n\t\t2. disp";
cout<<"\n\t\t3. search by reg";
cout<<"\n\t\t4. edit";
cout<<"\n\t\t5. delete";
cout<<"\n enter UR selection";
cin>>z;
switch(z)
{ case 1:
ofstream ifile("stud.dat",ios::app|ios::binary);
if(!ifile)
{cout<<"cannot open";
getch();
exit(0);
}
cout<<"\nenter details of student\n";
s.read();
ifile.write((char*)&s,sizeof(s));
ifile.close();
cout<<"\nrecord saved\n";
break;
case 2:
cout<<"\nall details\n";
ifstream ofile("stud.dat",ios::binary|ios::out);
ofile.read((char*)&s,sizeof(s));
while(!ofile.eof())
{ s.disp();
ofile.read((char*)&s,sizeof(s));
} ofile.close();
break;
case 3:
cout<<"\nenter reg no:";
cin>>sreg;
ifstream sfile("stud.dat",ios::binary|ios::out);
sfile.read((char*)&s,sizeof(s));
while(!sfile.eof())
{
if(sreg==s.retreg())
s.disp();
sfile.read((char*)&s,sizeof(s));
} sfile.close();
break;
case 4:
cout<<"enter rg no";
cin>>sreg;
ifstream efile("stud.dat",ios::binary|ios::out);
if(!efile)
{ cout<<"can not";
getch();
exit(0);
}
ofstream tfile("tstud.dat",ios::binary|ios::app);
if(!tfile)
{ cout<<"can not";
getch();
exit(0);
}
efile.read((char*)&s,sizeof(s));
while(!efile.eof())
{
if(sreg==s.retreg())
{
s.disp();
cout<<"enter new details";
s.edit();
tfile.write((char*)&s,sizeof(s));
}
else
tfile.write((char*)&s,sizeof(s));
efile.read((char*)&s,sizeof(s));
} efile.close();tfile.close();
remove("stud.dat");
rename("tstud.dat","stud.dat");
remove("tstud.dat");
cout<<"\nrecord edited\n";
break;
case 5:
cout<<"enter rg no";
cin>>sreg;
ifstream dfile("stud.dat",ios::binary|ios::out);
if(!dfile)
{ cout<<"can not";
getch();
exit(0);
}
ofstream dtfile("tstud.dat",ios::binary|ios::app);
if(!dtfile)
{ cout<<"can not";
getch();
exit(0);
}
dfile.read((char*)&s,sizeof(s));
while(!dfile.eof())
{
if(sreg==s.retreg())
{
s.disp();
cout<<"\nrecord deleted\n";
}
else
{
dtfile.write((char*)&s,sizeof(s));
}
dfile.read((char*)&s,sizeof(s));
} dfile.close();dtfile.close();
remove("stud.dat");
rename("tstud.dat","stud.dat");
remove("tstud.dat");
break;
}
cout<<"\ndo U want to continue(y/n)";
cin>>se;
clrscr();
}while(se=='y');
getch();
}
// for writing by using seekp
s.read();
file.seekp(((count-1)*sizeof(s)),ios::beg);
file.write((char*)&s,sizeof(s));
cout<<"\nrecord saved\n";
// for tellp tell the position in writing time
cout<<file.tellp();
//output is current count*size of class
// for display by using seekg
file.seekg(((rec-1)*sizeof(s)),ios::beg);
file.read((char*)&s,sizeof(s));
s.disp();
// for tellp tell the position in reading time
cout<<file.tellg();
//output is current count*size of class
dos shel commands
//dos commands
copy con <filename> //for creating
(^ z for saving)
type <filename> // for displaying
del <filename> // for deleting
exit // for closing
dir *.* // for all files