Q:

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

belongs to collection: C++ programs on various topics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to write and read time in/from binary ... >>
<< C++ program to demonstrate example of Templates....