Home »
Code Snippets »
C/C++ Code Snippets
C++ - Read Characters from One file and Write them in Toggle Case in Other using C++ file stream.
In this code snippet we will learn how to write characters in toggle case read from one file using c++ file stream.
C++ Code Snippet - Read Characters from One file and Write in Other in Toggle Case
#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
//creating a file and writing smething
fout.open("alex.txt",ios::out);
if(!fout){
cout<<"Error\n";
return -1;
}
//write text into file
fout<<"Hello World.";
fout.close();
//////////////////////////////////////
fin.open("alex.txt",ios::in);
fout.open("new.txt",ios::out);
if(!fin||!fout){
cout<<"ERROR\n";
return -1;
}
char ch;
while(fin)
{
if(fin.get(ch))
{
if(isupper(ch))
ch+=32;
else if(islower(ch))
ch-=32;
}
fout.put(ch);
}
fin.close();
fout.close();
//print the content of net.txt
fin.open("new.txt",ios::in);
if(!fin){
cout<<"Error";
return -1;
}
cout<<"Content of new.txt file :\n";
while(fin){
if(fin.get(ch))
cout<<ch;
}
cout<<endl;
fin.close();
///////////////////////////////
return 0;
}
Output
Content of new.txt file:
hELLO wORLD.