C++ - Read Characters from One file and Write them in Toggle Case in Other using C++ file stream.


IncludeHelp 29 August 2016

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;
}
    Content of new.txt file:  
    hELLO wORLD.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.