//Downloaded From theonlinetutorials.com
//A program to illustrate text file handling
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
char str[80],ch;
int opt,len[10];
do
{
cout<<"\nMenu:\n\n1.Write into file\n2.Read from file\n3.Display text\n\n";
cout<<"Enter option:";
cin>>opt;
switch(opt)
{
case 1:ofstream ofl; //For writing into a file
ofl.open("text.txt",ios::out);
if(!ofl)
cout<<"\nFile cannot be opened!";
cout<<"\nEnter a string:";
gets(str);
for(int i=0;str[i]!='\0';i++)
ofl.put(str[i]);
ofl.put('\n');
ofl.close();
break;
case 2:ifstream ifl; //For reading into a file
ifl.open("text.txt",ios::in);
if(!ifl)
cout<<"File cannot be opened!";
while(ifl)
{
ifl.getline(str,80);
cout<<str<<endl;
ifl.close();
}
break;
case 3:ifstream ifl2("text.txt"); //Outputting text
if(!ifl2)
cout<<"\nCannot open file!";
ofstream ofl2;
ofl2.open("texttoggle.txt");
if(!ofl2)
cout<<"File cannot be oppened!";
while(ifl2) //Converting string
{
ifl2>>str;
if(isupper(str[0]))
str[0]=tolower(str[0]);
str[0]=toupper(str[0]);
ofl2<<str;
cout<<str<<"";
}
cout<<endl;
ofl2.close();
ifl2.close();
break;
default:cout<<"\nWrong option!";
}
cout<<"Do you want to continue or not(y/n):";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
Not Satisfied ? Just search & get the result
Related posts:
