C++ Programming
Topics Covered=File Handling in C++
File Handling in C++
Sequential file handling
Program 248
//248.Example of checking validity of a file name and reading all characters but displaying only
Uppercase alphabet from file.
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream inn("c:\\turboc3\\file\\f48.txt"); // Ice Cream 123 (data of f48.txt)
clrscr();
if(!inn)
{
cout<<endl<<"Invalid File Name";
}
else
{
cout<<endl<<"Data of file is=\n";
while(!inn.eof()) //IcE Cream 123
{
c=inn.get(); //c=I
if(c>='A' && c<='Z')
cout<<c;
}
inn.close();
}
getch();
}
/*Output
Data of file is=
IEC */
Program 249
//249.Example of reading complete sentence from file.
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream inn("c:\\turboc3\\file\\f49.txt"); //Aryan College(data of f49.txt)
char nm[50];
clrscr();
if(!inn)
1|Page 31-12-2020
C++ Programming
{
cout<<endl<<"Invalid File Name";
}
else
{
/*char ch[100];
cout<<”enter a string=”;
cin.getline(ch,50); */
inn.getline (nm,50);
//displaying data from string (nm) using loop
for(int i=0;nm[i]!='\0';i++)
cout<<nm[i];
//displaying data from string (nm)
cout<<endl<<nm; //Aryan College
inn.close();
}
getch();
}
/*Output
Aryan College
Aryan College */
Program 250
//250.Example of Accepting file name from user and reading data from that file
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char nm[50];
cout<<"\n Enter name of file along with Path=";
cin.getline(nm,50); //input=c:/turboc3/file/f49.txt
ifstream i(nm);
if(!i)
{
cout<<endl<<"Invalid File Name or Path";
}
else
{
//Aryan College(data of f49.txt)
cout<<endl<<"Data of file is=";
i.getline(nm,50);
cout<<endl<<nm;
i.close();
}
getch();
}
/*Output
Data of file is=
Aryan College */
Program 251
2|Page 31-12-2020
C++ Programming
//251.Example of writing data into file
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char nm[50];
ofstream n("c:/turboc3/file/f51.txt");
char c='A';
n.put(c); //This put function puts data into file
n.close();
getch();
}
/*Output(in the file f51.txt)
A */
Program 252
//252.Example of writing multiple data into file
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char nm[50];
ofstream n("c:/turboc3/file/f52.txt");
char c='A';
n.put(c);
n.put('D');
c='$';
n.put(c);
n.close();
getch();
}
/*Output(data in f52.txt)
AD$ */
Program 253
//253.Example of Accepting data from user and writing that data into file
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char nm[50];
ofstream n("c:/turboc3/file/f53.txt");
char c='A';
n.put(c);
cout<<endl<<"Enter a character";
cin>>c; //input=H
n.put(c);
n.close();
3|Page 31-12-2020
C++ Programming
getch();
}
/*Output(data in f53.txt)
AH */
Program 254
//254.Example of Writing string into file.
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char nm[50]= "Ice Cream";
ofstream n("c:/turboc3/file/f54.txt");
for(int i=0;nm[i]!='\0';i++)
{
char c=nm[i];
n.put(c);
}
n.close();
getch();
}
/*Output(data in f54.txt)
Ice Cream */
4|Page 31-12-2020