A program to give input and output in a file using switch cases

This is a c++ program created through switch case in which the user gets a switching option of creating a file and displaying the elements of the file. This program uses the funtions to open and cloase a file. Through this program a student can clarify his\her difficulties in defining a file. In this program new header files too are used such as process.h, fstream.h and few more. File is actually a very essential part of c++ so this can be clarified by this simple and basic program. A student can enhance his\her knowledge in file concept. So friends without wasting much of our time lets start with the program the program is as follows:

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;

do
{
cout<<"\n\t1.Enter Text\n\t2.Show Text\n\t3.Exit";
cin>>ch;
switch(ch)
{
    case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\nEnter The Text ";
gets(str);
ofile<<str;
ofile.close();
char tmp[20];
afile.open("smp.txt",ios::in);
ofile.open("vwl.txt",ios::out);
while(!afile.eof())
{
afile.getline(tmp,20,' ');
if(tmp[0]=='a'||tmp[0]=='e'||tmp[0]=='i'||tmp[0]=='o'||tmp[0]=='u')
{
ofile<<tmp;
ofile<<' ';
}
}
afile.close();
ofile.close();

break;
    case '2' : cout<<"\nFormatted text:\t";
afile.open("vwl.txt",ios::in);
while(afile)
{
afile.get(ch);
cout<<ch;
}
afile.close();
break;
    case '3' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
  }while(ch1=='Y'||ch1=='y');
getch();
}

Share this

Related Posts

Previous
« Prev Post