Sunteți pe pagina 1din 8

PROGRAMS ON FILE HANDLING:

1.Write a c++ program to accept a file and determine its size in bytes.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{
clrscr();
char cont[100];
ofstream fout("arnab.txt",ios::out);
cout<<"\nenter contents of file\n";
gets(cont);
fout<<cont;
fout.close();
int nbytes=0;
char ch;
ifstream fin("arnab.txt");
while(!fin.eof())
{
if(fin.get(ch))
nbytes++;
}
cout<<"FILE SIZE-"<<nbytes<<"bytes";
fin.close();
}

2.Write a c++ file to accept a file and count how many thisandthese present.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
int i;
char a[200];
ofstream fout;
fout.open("article.txt",ios::out);
gets(a);
fout<<a;
fout.close();
void count();
{
ifstream fin;
fin.open("article.txt",ios::in);
char word[50];
int c1=0,c2=0;
while(!fin.eof())
{
fin>>word;
if(strcmp(word,"this")==0)
c1++;
if(strcmp(word,"these")==0)
c2++;
}
cout<<"this-"<<c1;
cout<<"these-"<<c2;
fin.close();
}
getch();
}

3.Write a c++ file to accept a file and send allthe names starting witha to another
file.

include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
struct student{ int rollno;
char name[3];
int marks;
char grade;
}a,b;
int c=0;
ofstream fout;
fout.open("marks.txt",ios::out);
for(int i=1;i<=2;i++)
{
cout<<"\n\nRoll : ";
cin>>a.rollno;
cout<<"\nName : ";
gets(a.name);
cout<<"\nMarks : ";
cin>>a.marks;
cout<<"\nGrade : ";
cin>>a.grade;
//fout<<a.rollno<<"\t";
//fout<<a.name<<"\t";
//fout<<a.marks<<"\t";
//fout<<a.grade<<"\t";
fout.write((char *)&a,sizeof (a));
}
fout.close();
ifstream fin("marks.txt",ios::in);
ofstream ob("students.txt",ios::out);
while(!fin.eof())
{
fin.read((char*)&b,sizeof (b));
if(b.name[0]=='a')
{
cout <<"\n\nC= "<<c<<" b.name= "<<b.name[0];
ob<<b.rollno<<"\t";
ob<<b.name<<"\t";
ob<<b.marks<<"\t";
ob<<b.grade<<"\t";
b.name[0]=NULL; // added this line to erase the buffer
c=c+1;
}

}
cout<<"\n\n\n"<<c<<" results transferred to students file";
fin.close();
ob.close();
getch();
}

4.Write a c++ program to accept the same file in previous program and count how
many have passed.
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>

void main()
{
struct stu{ int rollno;
char name[3]; // previously it ws name[10]
int marks;
char grade;
}b;
ifstream fin("marks.txt",ios::in);
b.marks=0;
clrscr();
while(!fin.eof())
{
fin.read((char *)&b,sizeof(b));
if(b.marks>=50)
{
cout<<"\n\n\nroll no "<<b.rollno;
cout<<"\nMarks="<<b.marks;
cout<<"\n\nPassed";
}
b.marks=0;
}
fin.close();
getch();
}

5.Write a c++ program to accept a file. Accept a roll no from the user and send the
details corresponding to that roll no to another file and display.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

#include<fstream.h>
#include<stdlib.h>
class student{
int rollno;
char name[10];
int Class;
float marks;
char grade;
public:
void getdata()
{
cout<<"\nrollno\n";
cin>>rollno;
cout<<"\nname\n";
gets(name);
cout<<"\nclass\n";
cin>>Class;
cout<<"\nmarks\n";
cin>>marks;
cout<<"\ngrade\n";
cin>>grade;
}
void putdata()
{
cout<<"roll
no:"<<rollno<<"\nname:"<<name<<"\nclass:"<<Class<<"\nmarks:"<<marks<<"
\ngrade:"<<grade;
}
int getrno()
{
return rollno;
}
void modify();
};
void student::modify()
{
cout<<"enter new details\n";
char nm[20]=" ";
int cl;
float mks;
cout<<"\nenter new name(enter'.'to retain)";
cin>>nm;
cout<<"\nenter new class(type same class to retain)";
cin>>cl;
cout<<"\nenter new marks(press -1 to retain)";
cin>>mks;
if(strcmp(nm,".")!=0);
strcpy(name,nm);
if(cl!=Class)
Class=cl;

if(mks!=-1)
marks=mks;
}
void main()
{
ofstream f("stu.dat",ios::out);
student s1;
int rno;
char found='f';
for(int a=1;a<=2;a++)
{
s1.getdata();
f.write((char*)&s1,sizeof(s1));
s1.putdata();
}
f.close();
cout<<"\nenter roll no to modify\n";
cin>>rno;
ifstream f1("stu.dat",ios::in);
ofstream f2("student.dat",ios::out);
while(!f1.eof())
{
f1.read((char*)&s1,sizeof(s1));
if(s1.getrno()==rno)
{
s1.modify();
cout<<"working";
getch();
f2.write((char*)&s1,sizeof(s1));
found='t';
}
}
if(found=='f')
cout<<"record not found\n";
f2.close();
ifstream f3("student.dat");
cout<<"now the file contains:\n";
while(!f3.eof())
{
f3.read((char*)&s1,sizeof(s1));
s1.putdata();
}
f3.close();
f1.close();
getch();
}

S-ar putea să vă placă și