Tuesday, October 25, 2011

FILING IN C./C++ SYNTAX OFSTREAM IFSTREAM EXAMPLE

Assuming that a text file named FIRST.TXT contains some text written into it, write a function named copyupper ( ), that reads the file FIRST.TXT and creates a new file named SECOND.TXT contains all words from the file FIRST.TXT in uppercase.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void copyuppercase();
main()
{
     
      copyuppercase();
      system("pause");     
}

void copyuppercase()
{
           ifstream myFile("first.txt");
           char a[40],ans;
           int count=0;
          
           while(!myFile.eof())
           {
                               myFile>>a;                   
                               count=strlen(a);
                               for(int i=0; i<count; i++)
                               {
                                   
                                    if(a[i]>=65 && a[i]<=90)
                                    ans=a[i]+32;
                                    else if (a[i]>=97 && a[i]<=122)
                                    ans=a[i]-32;
                                   
                                    ofstream myfile;
                                    myfile.open("second.txt", ios::app);
                                    myfile<<ans;
                                    myfile.close();
                               }
          
           }
          
}

No comments:

Post a Comment