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();
                               }
          
           }
          
}

Monday, October 24, 2011

FILINF IN C/C++ SYNTAX EXAMPLE FSTREAM, OFSTREAM

Write a function to count the number of blank present in a text file named "OUT.TXT

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
main()
{
      int count=0,countsp=0;
      ifstream myFile("Out.txt");
      char a[40];
      while(!myFile.eof())
                    {
                               myFile >> a;
                               
                               //count=strlen(a);
                               cout<<a<<" ";
                               countsp++;
                              
                    }
countsp--;
cout<<endl;
cout<<"\nThere are total of "<<countsp<<" Spaces in this file\n\n";
system("pause");     
}

Saturday, October 22, 2011

FILING IN C/C++ SYNTAX

Write a user-defined function in C++ to read the content from a text file OUT.TXT, count and display the number of alphabets present in it.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void countDisplay();
main()
{
countDisplay();     
}
      void countDisplay()
      {
      int count=0,ans=0;
      ifstream myFile("Out.txt");
      char a[40];
      while(!myFile.eof())
                    {
                               myFile >> a;
                               count=strlen(a);
                               ans=ans+count;
                               cout << a<<endl;
                    }
cout<<"\nThere are total of "<<ans<<" Characters in this file\n\n";
system("pause");
}

Tuesday, October 11, 2011

WRITE A C++ PROGRAM TO WRITE NUMBERS FROM 1 TO 100 IN A FILE

This program is using filing concept, and will make a file called notes.txt and generate numbers from 1 to 100 using for loop.

#include<iostream>
#include<fstream>
using namespace std;
main()
{
      ofstream myFile;
      myFile.open("Notes.txt", ios::app);
      for(int i=1; i<=100; i++)
      {
              myFile<<i<<" ";
      }
      myFile.close();
system("pause");
}