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.




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.