Академический Документы
Профессиональный Документы
Культура Документы
Puram
fstream.h
iostream.h
ios
cin
istream
ostream
cout
ifstream
ofstream
fstream
Data File
Binary File
Text File
Rno
1
2
3
4
8
News Today
July 12, 2011
Donald and Micky agreed
upon the issue of having
lunch together on the
occasion of Friendship
Day. It is good news for
Disney land.
void CreateText()
{
ofstream F("mytext.txt");
F<<"News Today"<<endl;
F<<"July 12,2011"<<endl;
F<<"Donald and Micky"<<endl;
F.close();
}
void ReadText()
{
ifstream F("mytext.txt");
char Str[80];
F.getline(Str,80);
cout<<Str<<endl;
F.close();
}
Name
Atin Suri
Kanika Priya
Ravish Shah
Harish Dey
Tejvir
Fees
6000
6500
6000
5500
6000
class Student
{
int Rno;char Nam[20];float Fees;
public:
void Read()
{cin>>Rno;gets(Nam);cin>>Fees;}
void Disp()
{cout<<Rno<<":"<<Nam<<":"<<Fees<<endl;}
};
void CreatBinFile()
{
fstream F;
F.open("EMP",ios::binary|ios::out);
Student S;
S.Read();//Getting a record from user
F.write((char*)&S,sizeof(S));
F.close();
}
void ReadBinFile()
{
fstream F;
F.open("EMP",ios::binary|ios::in);
Student S;
F.read((char*)&S,sizeof(S));
S.Disp();
F.close();
}
#1
Alternative*
void Create()
ofstream Fil(DIARY.TXT);
{
fstream Fil;
Fil.open("DIARY.TXT",ios::out);
char Lin[80],Q;
Opening a file in out Mode*
(Deletes/Overwrites the earlier content, if present)
do
{
cout<<Enter Text;gets(Lin); //User inputs the data in a string Lin
Fil<<Lin<<endl;
//Writing of content of Lin on file
cout<<More(Y/N)?;cin>>Q;
}
while (Q==Y);
Fil.close();
DIARY.TXT
}
Hello my dear computer
I will save this
2
Text on the hard disk
Lin
USER
F<<Lin<<endl;
Text on the hard disk
gets(Lin);
1
Alternative**
ifstream Fil(DIARY.TXT);
DIARY.TXT
Hello my dear computer
I will save this
Text on the hard disk
USER
1
Fil.getline(Lin,80)
2
cout<<Lin<<endl;
Lin
Hello my dear computer
Fil>>WRD;
Reading a word at a time from a text file
Fil.getline(String,80) A
Checking end of file
while (!Fil.eof())
File Handling in C++ Part 1 Mukesh Kumar
[REF:DPSR/COMP12/2012-2013/08]
#2
void WordDisplay()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::in);
char WORD[40];
while (!Fil.eof())//Checks for End of File
{
Fil>>WORD;
//Reading a word from File
cout<<WORD<<endl;
}
Fil.close();
}
int CountWord()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::in);
char WORD[40]; int CW=0;
while (!Fil.eof())//Checks for End of File
{
Fil>>WORD;
//Reading a word from File
CW++;
}
Fil.close();
return CW;
}
void ReverseWord()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::in);
char WORD[40];
while (!Fil.eof())//Checks for End of File
{
Fil>>WORD;
//Reading a word from File
for (int I=strlen(WORD)-1;I>=0;I--)
cout<<WORD[I];
cout<<endl;
}
Fil.close();
}
void ReverseLine()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::in);
char Lin[80];
while (Fil.getline(Lin,80))
{
for (int I=strlen(Lin)-1;I>=0;I--)
cout<<Lin[I];
cout<<endl;
}
Fil.close();
}
void VowelCount()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::in);
char Ch;
int NV=0,NC=0;
while (!Fil.eof())//Checks for End of File
{
Ch=Fil.get(); //Reading a character from File
if (isalpha(Ch))
{
Ch=toupper(Ch);
if (Ch==A||Ch==E||Ch==I||
Ch==O||Ch==U)
NV++;
else
NC++;
}
}
cout<<Vowels:<<NV<<endl;
cout<<Consonants:<<NC<<endl;
Fil.close();
}
void AddAtEnd()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::app);
char Lin[80],Q;
do
{
cout<<Enter Text;
gets(Lin);
Fil<<Lin<<endl;
cout<<More(Y/N)?;
cin>>Q;
}
while (Q==Y);
Fil.close();
}
#3