Basic Program- File handling

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class drinks
{public:
int code;
char name[30];

void getdata()
{
cin>>code;
cout<<“\n”;
gets(name);
}
void showdata()
{
cout<<code<<“\n”;
puts(name);
}
};
void main()
{
clrscr();
drinks d;
char choice;
fstream myfile;
myfile.open(“drinks.dat”,ios::out);
do
{
cout<<“enter details”;
d.getdata();
myfile.write((char*)&d,sizeof(drinks));
cout<<” more????”;
cin>>choice;
}
while((choice==’y’)||(choice==’Y’));
myfile.close();
char search[30];

cout<<” enter name”;
gets(search);
fstream myfil;
myfil.open(“drinks.dat”,ios::in);
myfil.seekg(0);
int found=0;
while(myfil)
{
myfil.read((char*)&d,sizeof(drinks));
if(strcmp(d.name,search)==0)
{
d.showdata();
found=1;
break;
}
}
if(found==0)
{
cout<<“not found”;
}
myfil.close();
getch();
}

Finding a specific word in a file using file handling

//word to be found: do

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
char choice;
fstream myfile;
myfile.open(“drinks.dat”,ios::out);
char row[80];
do
{
cout<<“enter details”;
gets(row);
myfile<<row<<“\n”;
cout<<” more????”;
cin>>choice;
}
while((choice==’y’)||(choice==’Y’));
myfile.close();

fstream myfil;
myfil.open(“drinks.dat”,ios::in);
myfil.seekg(0);
char ch1,ch2;
int found=0;
char r[80];
while(myfil)
{
cout<<“file is”;
myfil.get(r,80);
puts(myfil);
cout<<“\n”;
if((ch1==’d’)&&(ch2==’o’))
{found++;
}
}
if(found==0)
{
cout<<“not found”;
}
else{
cout<<found;
}
myfil.close();
getch();
}

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives