Member Function of Stream Classes
1. ifstream class : - It provide input operations. It contain
open() function and inherit get() , getline() , read() ,
seekg() , tellg() functions from istream class.
2. ofstream class : - It provide output operations. It contain
open() function and inherit put() , write() , seekp() , tellp()
functions from ostream class.
3. fstream class :- It provide both input and output operations
and it inherit all functions of istream and ostream classes
through iostream class.
Reading data from file
There are four methods to read data from file:
1- using >> operator
2- using get() function
3- using getline() function
4- using read() function
Writing data in file
There are three methods to read data from file:
1- using << operator
2- using put() function
3- using write() function
Program : Writing data in file using << operator
#include<fstream.h>
void main()
{
ofstream fout;
int roll; float marks; char ch = ‘y’;
fout.open(“student.txt”,ios::out);
while(ch==‘y’ || ch == ‘Y’)
{
cout<<“Enter Roll Number”;
cin>>roll;
cout<<“nEnter Marks”;
cin>>marks;
fout<<roll<<“n”<<mark<<“n”; //cascading of << operator in case of file
cout<<“nDo you want to add more data:”;
cin>>ch;
}
fout.close();
}
Program : Reading data from file using >> operator
#include<fstream.h>
void main()
{
ifstream fin;
int roll; float marks;
fin.open(“student.txt”,ios::in);
while(fin>>roll>>marks) //cascading of >> operator in case of file
{
cout<<roll<<“t”<<mark<<“n”;
}
fin.close();
}
getch();
}
Reading data from file using get() function
There are two forms of get() function:
1- get(char):- This method read a single character at a time from
input stream including space and new line(n).
char ch;
fin.get(ch);
2- get(array , size):- It read the specified number of characters
from inpput stream(if new line is not encountered) and if new
line is encountered then it read all the characters before n
and n character is left in input stream.
char name[20];
fin.get(name , 20);
Program : Reading data from file using get() function
#include<fstream.h>
void main()
{
fstream fout; char ch;
fout.open(“Name.txt”,ios::out);
if(!fout)
cout<<“nError in opening file”;
fout<<“Ankit Kumar”<<“n”;
fout<<“Mohit”<<“n”;
fout<<“Abhishek Sharma”;
fout.close();
fout.open(“Name.txt”,ios::in)
while(fout.get(ch))
{
cout<<ch;
}
fout.close();
}
Program: Read data from one file and then copy into another file(To take backup)
#include<fstream.h>
void main()
{
ofstream fout;
ifstream fin;
char ch;
char p[30];
clrscr();
fin.open("Name.txt",ios::in);
fout.open("backup.txt",ios::out);
if(!fin)
cout<<"nError in opening file" ;
if(!fout)
cout<<"nUnable to open file“;
while(fin.get(ch))
{
fout.put(ch); // to write in file
}
fout.close();
fin.close();
cout<<"ndata in backup file....n";
fin.open("backup.txt",ios::in);
while(fin.get(ch))
{
cout<<ch;
}
fin.close();
getch();
}
Reading data from file using getline() function
getline() function reads a sequence of
characters till new line character(n) from input
stream and insert null character(0) after the
line in character array. The getline() function
removes the delimiter(n) from input stream.
“while get() function does not remove the new
line character(n) from input stream”
Program : Reading data from file using getline() function
#include<fstream.h>
void main()
{
fstream fout;
char str[30];
fout.open(“Name.txt”,ios::out);
if(!fin)
{ cout<<“nError in opening file”; }
fout<<“Ankit Kumar”<<“n”;
fout<<“Mohit”<<“n”;
fout<<“Abhishek Sharma”<<“n”;
fout.close();
fout.open(“Name.txt”,ios::in)
while(fout.getline(str,30))
{
cout<<str<<endl;
}
fout.close();
}
Detecting end-of-file condition
There are two method to detect end-of-file condition :
1- using stream class object :-
ifstream fin;
while(fin)
{ ----
}
2- using eof() function :- This function returns true(non-zero) when
end-of-file condition is encountered otherwise it returns zero.
while(!fin.eof())
{ ----
}
Program:Write a function to count number of alphabets in a text file “Data.txt”
#include<fstream.h>
#include<ctype.h>
void countalpha()
{
ifstream fin;
char ch;
int a=0;
fin.open(“Data.txt”,ios::in);
while(fin.get(ch))
{
if(isalpha(ch))
a++;
}
cout<<“nTotal Alphabets:”<<a;
fin.close();
}
Program:Write a function to count number of uppercase alphabets in a
text file “Data.txt”
#include<fstream.h>
#include<ctype.h>
void countupper()
{
ifstream fin;
char ch;
int a=0;
fin.open(“Data.txt”,ios::in);
while(fin)
{
fin.get(ch);
if(isupper(ch))
a++;
}
cout<<“nTotal Uppercase Alphabets:”<<a;
fin.close();
}
Program:Write a function to count the words “the” and “to” in a text file “Data.txt”
#include<fstream.h>
#include<string.h>
void countwords()
{
ifstream fin;
char str[20];
int a=0,b=0;
fin.open(“Data.txt”,ios::in);
while(!fin.eof())
{
fin>>str;
if(strcmp(str,”the”)==0)
a++;
if(strcmp(str,”to”)==0)
b++;
}
cout<<“nTotal number of “the” in file:”<<a;
cout<<“nTotal number of “to” in file:”<<b;
fin.close();
}
Program:Write a function to count number of lines starting with alphabet ‘A’
in a text file “Data.txt”
#include<fstream.h>
void countwords()
{
ifstream fin;
char str[50];
int a=0;
fin.open(“Data.txt”,ios::in);
while(fin.getline(str,50))
{
if(str[0]==‘A’)
a++;
}
cout<<“nTotal number of line starting with ‘A’ :”<<a;
fin.close();
}

PythonProgramming-Text-file-handling-1.pptx

  • 1.
    Member Function ofStream Classes 1. ifstream class : - It provide input operations. It contain open() function and inherit get() , getline() , read() , seekg() , tellg() functions from istream class. 2. ofstream class : - It provide output operations. It contain open() function and inherit put() , write() , seekp() , tellp() functions from ostream class. 3. fstream class :- It provide both input and output operations and it inherit all functions of istream and ostream classes through iostream class.
  • 2.
    Reading data fromfile There are four methods to read data from file: 1- using >> operator 2- using get() function 3- using getline() function 4- using read() function
  • 3.
    Writing data infile There are three methods to read data from file: 1- using << operator 2- using put() function 3- using write() function
  • 4.
    Program : Writingdata in file using << operator #include<fstream.h> void main() { ofstream fout; int roll; float marks; char ch = ‘y’; fout.open(“student.txt”,ios::out); while(ch==‘y’ || ch == ‘Y’) { cout<<“Enter Roll Number”; cin>>roll; cout<<“nEnter Marks”; cin>>marks; fout<<roll<<“n”<<mark<<“n”; //cascading of << operator in case of file cout<<“nDo you want to add more data:”; cin>>ch; } fout.close(); }
  • 5.
    Program : Readingdata from file using >> operator #include<fstream.h> void main() { ifstream fin; int roll; float marks; fin.open(“student.txt”,ios::in); while(fin>>roll>>marks) //cascading of >> operator in case of file { cout<<roll<<“t”<<mark<<“n”; } fin.close(); } getch(); }
  • 6.
    Reading data fromfile using get() function There are two forms of get() function: 1- get(char):- This method read a single character at a time from input stream including space and new line(n). char ch; fin.get(ch); 2- get(array , size):- It read the specified number of characters from inpput stream(if new line is not encountered) and if new line is encountered then it read all the characters before n and n character is left in input stream. char name[20]; fin.get(name , 20);
  • 7.
    Program : Readingdata from file using get() function #include<fstream.h> void main() { fstream fout; char ch; fout.open(“Name.txt”,ios::out); if(!fout) cout<<“nError in opening file”; fout<<“Ankit Kumar”<<“n”; fout<<“Mohit”<<“n”; fout<<“Abhishek Sharma”; fout.close(); fout.open(“Name.txt”,ios::in) while(fout.get(ch)) { cout<<ch; } fout.close(); }
  • 8.
    Program: Read datafrom one file and then copy into another file(To take backup) #include<fstream.h> void main() { ofstream fout; ifstream fin; char ch; char p[30]; clrscr(); fin.open("Name.txt",ios::in); fout.open("backup.txt",ios::out); if(!fin) cout<<"nError in opening file" ; if(!fout) cout<<"nUnable to open file“; while(fin.get(ch)) { fout.put(ch); // to write in file } fout.close(); fin.close(); cout<<"ndata in backup file....n"; fin.open("backup.txt",ios::in); while(fin.get(ch)) { cout<<ch; } fin.close(); getch(); }
  • 9.
    Reading data fromfile using getline() function getline() function reads a sequence of characters till new line character(n) from input stream and insert null character(0) after the line in character array. The getline() function removes the delimiter(n) from input stream. “while get() function does not remove the new line character(n) from input stream”
  • 10.
    Program : Readingdata from file using getline() function #include<fstream.h> void main() { fstream fout; char str[30]; fout.open(“Name.txt”,ios::out); if(!fin) { cout<<“nError in opening file”; } fout<<“Ankit Kumar”<<“n”; fout<<“Mohit”<<“n”; fout<<“Abhishek Sharma”<<“n”; fout.close(); fout.open(“Name.txt”,ios::in) while(fout.getline(str,30)) { cout<<str<<endl; } fout.close(); }
  • 11.
    Detecting end-of-file condition Thereare two method to detect end-of-file condition : 1- using stream class object :- ifstream fin; while(fin) { ---- } 2- using eof() function :- This function returns true(non-zero) when end-of-file condition is encountered otherwise it returns zero. while(!fin.eof()) { ---- }
  • 12.
    Program:Write a functionto count number of alphabets in a text file “Data.txt” #include<fstream.h> #include<ctype.h> void countalpha() { ifstream fin; char ch; int a=0; fin.open(“Data.txt”,ios::in); while(fin.get(ch)) { if(isalpha(ch)) a++; } cout<<“nTotal Alphabets:”<<a; fin.close(); }
  • 13.
    Program:Write a functionto count number of uppercase alphabets in a text file “Data.txt” #include<fstream.h> #include<ctype.h> void countupper() { ifstream fin; char ch; int a=0; fin.open(“Data.txt”,ios::in); while(fin) { fin.get(ch); if(isupper(ch)) a++; } cout<<“nTotal Uppercase Alphabets:”<<a; fin.close(); }
  • 14.
    Program:Write a functionto count the words “the” and “to” in a text file “Data.txt” #include<fstream.h> #include<string.h> void countwords() { ifstream fin; char str[20]; int a=0,b=0; fin.open(“Data.txt”,ios::in); while(!fin.eof()) { fin>>str; if(strcmp(str,”the”)==0) a++; if(strcmp(str,”to”)==0) b++; } cout<<“nTotal number of “the” in file:”<<a; cout<<“nTotal number of “to” in file:”<<b; fin.close(); }
  • 15.
    Program:Write a functionto count number of lines starting with alphabet ‘A’ in a text file “Data.txt” #include<fstream.h> void countwords() { ifstream fin; char str[50]; int a=0; fin.open(“Data.txt”,ios::in); while(fin.getline(str,50)) { if(str[0]==‘A’) a++; } cout<<“nTotal number of line starting with ‘A’ :”<<a; fin.close(); }