Вы находитесь на странице: 1из 3

Delhi Public School, R.K.

Puram

Computer Science Class XII

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();
}

File Handling in C++ Part 1 Mukesh Kumar


[REF:DPSR/COMP12/2012-2013/08]

#1

Delhi Public School, R.K.Puram

Computer Science Class XII

Creating a TEXT file

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);

Displaying the content from TEXT File


void Display()
{
fstream Fil;
Fil.open("DIARY.TXT",ios::in);
char Lin[80];
while (Fil.getline(Lin,80))
cout<<Lin<<endl;
Fil.close();
}

Opening a file in in Mode**

//Reads a line/checks for end of file


//Displays a line of text

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

Reading a character from a text file


Ch=Fil.get()
B

Fil>>WRD;
Reading a word at a time from a text file

Reading a line from a text file

Fil.getline(String,80) A
Checking end of file

Sample operations that can be


performed on a text file opened
in in mode
A. Reading and displaying the
entire content of file
B. Reading each word and
displaying them in different
lines
C. Reading each character and
checking, if it is a Vowel or a
Consonant

while (!Fil.eof())
File Handling in C++ Part 1 Mukesh Kumar
[REF:DPSR/COMP12/2012-2013/08]

#2

Delhi Public School, R.K.Puram

Computer Science Class XII

Reading each word and displaying on different lines

Reading and counting the no. of words

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;
}

Reading each word, reversing and displaying

Reading each line, reversing and displaying

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();
}

Counting number of vowels and consonants

Adding new lines at the bottom of the file

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();
}

File Handling in C++ Part 1 Mukesh Kumar


[REF:DPSR/COMP12/2012-2013/08]

Opening a file in Append app Mode


(Moving file write pointer at the bottom
of the program without deleting the
earlier content)

#3

Вам также может понравиться