Professional Documents
Culture Documents
fstream.h ifstream(),ofstream(),read(),
write(),close(),tellg(),seekg()
process.h exit()
stdio.h remove(),rename(),gets()
conio.h getch(),clrscr()
string.h strcmpi()
3. FUNCTIONS:
i) ifstream(): It represents input stream
and this is used for reading data from files.
ii) ofstream(): It represents output stream
and this is used for writing the data in files.
iii) write():The write() function is used to
write object or record to the file. A record
may be an array, structure or class.
iv) read():The read() function is used to
read object to the file.
v) tellg(): tellg() is used to know where the
get pointer is in a file.
vi) seekg(): It is used to move the get pointer
to a desired location with respect to a
reference point.
vii) exit(): It is used to exit the program.
viii) remove(): The remove() function deletes a
file from memory.
ix) rename(): The rename() function takes the
name of the file as its argument and the new
name of the file as second argument.
x) strcmpi(): It is used to compare the string
with another string.
4. CODE
class music
{
#include<fstream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int sno;
char s_name[50] ,s_category[50], s_movie[50],
singer[50], language[50];
public:
void add()
{
cout<<"\n\t\t\tENTER THE NO. OF THE SONG : ";
cin>>sno;
cout<<"\n\t\t\tENTER SONG NAME:";
gets(s_name);
cout<<"\n\t\t\tENTER ALBUM/SONG MOVIE:";
gets(s_movie);
cout<<"\n\t\t\tENTER SONG CATEGORY:";
gets(s_category);
cout<<"\n\t\t\tENTER NAME OF THE SINGER:";
gets(singer);
cout<<"\n\t\t\tENTER LANGUAGE OF THE
SONG:";
gets(language);
}
void display()
{
cout<<"\nSong No : "<<sno;
cout<<"\nSong Name:"<<s_name;
cout<<"\nAlbum/Movie Name:"<<s_movie;
cout<<"\nCategory of the song:"<<s_category;
cout<<"\nSinger of the song:"<<singer;
cout<<"\nLanguage of the song:"<<language;
cout<<"\n-------------------------\n";
}
char *getname()
{
return s_name;
}
int getsong()
{
return sno;
}
}obj;
void main()
{
int choice;
clrscr();
cout<<"\n\t\t\t....... MAIN MENU...........\t\t\t\t";
cout<<"\n\t\t\t......1.ADD A RECORD........\t\t\t\t";
cout<<"\n\t\t\t......2.DISPLAY THE RECORD..\t\t\t\t";
cout<<"\n\t\t\t......3.SEARCH ANY RECORD ...\t\t\
t\t";
cout<<"\n\t\t\t......4.DELETE ANY RECORD...\t\t\t\t";
cout<<"\n\t\t\t......5.MODIFY THE RECORD . . .
\ t \ t \t\t";
cout<<"\n\t\t\t......6.EXIT................\t\t\t\t";
cout<<"\n\t\t\t ENTER YOUR CHOICE : ";
cin>>choice;
if(choice==1)
{
ofstream off;
off.open("RECORD.dat",ios::binary|
ios::app);
char ch='Y';
while(ch=='Y')
{
obj.add();
off.write((char*)&obj,sizeof(obj));
cout<<"\nSONG ADDED SUCCESSFULLY";
cout<<"\nWANT TO ENTER MORE RECORDS
Y/N";
cin>>ch;
}
off.close();
getch();
}
else if(choice==2)
{
clrscr();
ifstream iff("RECORD.dat",ios::in|ios::binary);
cout<<"\nYOUR PLAYLIST : ";
while(!iff.eof())
{
iff.read((char*)&obj,sizeof(obj));
obj.display();
}
iff.close();
getch();
}
else if(choice==3)
{
char name[50];
int count=0;
ifstream iff("RECORD.dat",ios::in|ios::binary);
cout<<"\nENTER THE NAME OF THE SONG YOU
WANT TO SEARCH : ";
gets(name);
while(!iff.eof())
{
iff.read((char*)&obj,sizeof(obj));
if(strcmpi(name,obj.getname())==0)
{
obj.display();
count++;
cout<<"\nSONG DETAILS DISPLAYED.";
}
}
if(count==0)
{
cout<<"\nSONG NOT FOUND";
}
iff.close();
getch();
}
else if(choice==4)
{
int no,count=0;
ifstream iff("RECORD.dat",ios::in|ios::binary);
ofstream off("TEMP.dat",ios::app|ios::binary);
cout<<"\nENTER THE No. OF THE SONG YOU
WANT TO DELETE : ";
cin>>no;
while(!iff.eof())
{
iff.read((char*)&obj,sizeof(obj));
if(no!=obj.getsong())
{
off.write((char*)&obj,sizeof(obj));
}
else
{
cout<<"\NSONG DELETED";
obj.display();
count++;
}
}
if(count==0)
{
cout<<"\nSONG NOT FOUND";
}
iff.close();
off.close();
remove("RECORD.dat");
rename("TEMP.dat","RECORD.dat");
getch();
}
else if(choice==5)
{
int no;
int flag=0;
long pos;
fstream off; off.open("RECORD.dat",ios::binary|
ios::out|ios::in);
cout<<"\nENTER THE No. OF THE SONG YOU
WANT TO MODIFY RECORD OF : \n";
cin>>no;
while(off.read((char*)&obj,sizeof(obj)))
{
pos=off.tellg();
if(obj.getsong()==no)
{
off.seekg(pos-sizeof(obj));
cout<<"\nEnter new details : \n";
obj.add();
flag++;
off.write((char*)&obj,sizeof(obj));
}
off.close();
if(flag==0)
{
cout<<"\nSONG NOT FOUND";
}
else
{
cout<<"\nRECORD MODIFIED";
}
getch();
}
else if(choice==6)
{
exit(0);
}
}