MIX OF MANY TEXT FILE OPERATIONS (Doing this program will complete your concept of Text File Handling)

//Text File Operations
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>

//Functioon Prototypes prototypes
void menu();
void writing();
void reading();
void rename();
void remove();
void copy();
void append();
void countlines();
void countwords();
void searching();
void replace();
void vowels();
void characters();
void list();
void compare();
void line();

void footer(char *s)
{
 cout<<endl;
 textcolor(random(13)+2+BLINK);
 cprintf("Data saved to file %s",s);
 textcolor(7);
 cout<<endl;
}

void menu()
{
 system("cls");
 cout<<"............... TEXT FILE OPTIONS ..............\n";
 cout<<"0.  EXIT........................................\n";
 cout<<"1.  CREATE A NEW FILE...........................\n";
 cout<<"2.  READ A FILE.................................\n";
 cout<<"3.  RENAME A FILE...............................\n";
 cout<<"4.  DELETE A FILE...............................\n";
 cout<<"5.  COPY A FILE.................................\n";
 cout<<"6.  COUNT NUMBER OF LINES IN FILE...............\n";
 cout<<"7.  COUNT NUMBER OF WORDS IN FILE...............\n";
 cout<<"8.  SEARCH A WORD IN FILE.......................\n";
 cout<<"9.  REPLACE A WORD IN FILE......................\n";
 cout<<"10. COUNT VOWELS IN FILE........................\n";
 cout<<"11. DISPLAY LETTER DETAILS OF FILE..............\n";
 cout<<"12. DISPLAY FILE | PAGE WISE ...................\n";
 cout<<"13. APPEND FILE.................................\n";
 cout<<"14. DISPLAY LIST OF FILES.......................\n";
 cout<<"15. COMPARE 2 FILES.............................\n";
 cout<<"    ENTER YOUR CHOICE ........................ : " ;
}

void writing()
{
 char name[20],ch;
 system("cls");
 cout<<"Enter filename to create : ";
 cin>>name;
 ofstream fout(name,ios::app|ios::out);
 do
 {
  ch=getchar();
  fout<<ch;
 }while(ch!='*');
 fout.close();
 footer(name);
}

void reading()
{
 char name[20], ch;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter filename to read from the above list: ";
 cin>>name;
 ifstream fin;
 fin.open(name, ios::in);
 while(!fin.eof())
 {
  fin.get(ch);
  cout<<ch;
 }
 footer(name);
 cout<<"\nFile Reading Completed...\n";
 fin.close();
}

void rename()
{
 char name[20], name1[20];
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter filename to rename with extension : ";
 cin>>name;
 cout<<"Enter new name for file with extension  : ";
 cin>>name1;
 rename(name,name1);
 cout<<"File : "<<name<<" has been renamed with : "<<name1<<endl;
 footer(name1);
}

void remove()
{
 char name[20];
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter filename with extension to delete : ";
 cin>>name;
 remove(name);
 cout<<"File : "<<name<<" deleted....\n";
}

void copy()
{
 char name[20], name1[20], ch;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter the filename to copy : ";
 cin>>name;
 cout<<"Enter the new name for file: ";
 cin>>name1;
 ifstream fin(name,ios::in);
 ofstream fout(name1,ios::out);
 while(!fin.eof())
 {
  ch=fin.get();
  fout<<ch;
 }
 fin.close();
 fout.close();
 cout<<"\nThe file "<<name<<" is copied with new name "<<name1<<endl;
 footer(name1);
}

void countlines()
{
 char name [20],ch[80];
 int count=0;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter filename with extension to count lines : ";
 cin>>name;
 ifstream fin(name,ios::in);
 while(!fin.eof())
 {
  fin.getline(ch,80,'.');
  count++;
 }
 cout<<"\nThere are "<<count<<" number of lines in "<<name<<" file\n";
 fin.close();
}

void countwords()
{
 char name[20], ch;
 int count=0;
 system("dir *.txt/w");
 cout<<"Enter filename with extension to count words : ";
 cin>>name;
 ifstream fin(name, ios::in);
 while(!fin.eof())
 {
  fin>>ch;
  count++;
 }
 cout<<"There are "<<count<<" number of words in "<<name<<" file\n";
 fin.close();
}

void searching()
{
 char word[20], name[20], ch[20];
 int flag = 0;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter a word you want to search   : ";
 cin>>word;
 cout<<"Enter name of file with extension : ";
 cin>>name;
 ifstream fin(name,ios::in);
 while(!fin.eof())
 {
  fin>>ch;
  if(strcmpi(ch,word)==0)
  {
   flag++;
  }
 }
 if(flag==0)
 {
  cout<<"The word "<<word<<" not found in "<<name<<" file\n";
 }
 else
 {
  cout<<"The word "<<word<<" found "<<flag<<" times in "<<name<<" file\n";
 }
 fin.close();
}

void replace()
{
 char word[20], repword[30], name[20], ch[20];
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter the filename : ";
 cin>>name;
 cout<<"Enter the word you want to find and replace : ";
 cin>>word;
 cout<<"Enter the new word to replace the found one : ";
 cin>>repword;
 ifstream fin(name, ios::in);
 while(!fin.eof())
 {
  fin>>ch;
  if(strcmpi(ch,word)==0)
  {
   cout<<repword<<" ";
  }
  else
  {
   cout<<ch<<" ";
  }
 }
 cout<<"The word "<<word<<" replaced with "<<repword<<" everywhere\n";
 fin.close();
}

void vowels()
{
 char ch, name[20];
 int count=0;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter filename to count vowels in it: ";
 cin>>name;
 ifstream fin(name,ios::in);
 while(!fin.eof())
 {
  fin.get(ch);
  if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
  {
   count++;
  }
 }
 cout<<"There are "<<count<<" vowels in "<<name<<" file.\n";
 fin.close();
}

void characters()
{
 char name[20],ch;
 int u=0,l=0,s=0,d=0,all=0,sp=0;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter name of file to count characters in it: ";
 cin>>name;
 ifstream fin(name,ios::in);
 while(!fin.eof())
 {
  fin.get(ch);
  if(isupper(ch))
  {
   u++;
  }
  else if(islower(ch))
  {
   l++;
  }
  else if(isdigit(ch))
  {
   d++;
  }
  else if(isspace(ch))
  {
   sp++;
  }
  else
  {
   s++;
  }
  all++;
 }
 cout<<"The Character summary of "<<name<<" file is shown below :\n";
 cout<<"Total number of characters with spaces    : "<<all<<endl;
 cout<<"Total number of characters without space  : "<<all-sp<<endl;
 cout<<"Total number of UPPER CASE characters     : "<<u<<endl;
 cout<<"Total number of lower case characters     : "<<l<<endl;
 cout<<"Total number of digits in a file          : "<<d<<endl;
 cout<<"Total number of special characters in file: "<<s<<endl;
 fin.close();
}

void line()
{
 char ch[80], name[20];
 int count=0,p=0;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter the filename to read pagewise : ";
 cin>>name;
 ifstream fin(name,ios::in);
 while(!fin.eof())
 {
        fin.getline(ch,80,'\n');
        cout<<ch<<endl;
        count++;
        if(count%22==0)
        {
   p++;
   cout<<"\tPage-"<<p<<".";
   system("pause");
        }
 }
 cout<<"\nThe "<<name<<" file reading completed...\n";
 fin.close();
}

void append()
{
 char ch, name[20];
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter the filename you want to append : ";
 cin>>name;
 fstream fio(name, ios::in|ios::out|ios::app);
 cout<<"\nThe contents of file: "<<name<<" is shown herewith.";
 cout<<"You may continue to append in it & press * to end...\n";
 while(!fio.eof())
 {
  ch=fio.get();
  cout<<ch;
 }

  do
 {
  ch=getchar();
  fio<<ch;
 }while(ch!='*');
 fio.close();
 footer(name);
}

void list()
{
 system("cls");
 system("dir *.txt");
}

void compare()
{
 char name[20], name1[20], ch1, ch2;
 int s1=0, s2=0, flag=1;
 system("cls");
 system("dir *.txt/w");
 cout<<"Enter the first file name to compare: ";
 cin>>name;
 cout<<"Enter the other file name to compare: ";
 cin>>name1;
 ifstream fin1(name, ios::in);
 ifstream fin2(name1,ios::in);

 while(!fin1.eof())
 {
ch1=fin1.get();
s1++;
 }
 fin1.close();
 while(!fin2.eof())
 {
ch2=fin2.get();
s2++;
 }
 fin2.close();

 fin1.open(name,ios::in);
 fin2.open(name1,ios::in);

 if(s1==s2)
 {
  cout<<"\nBoth files have similar length : "<<sizeof(fin1)<<" bytes...\n";
  while(!fin1.eof())
  {
   ch1=fin1.get();
   ch2=fin2.get();
   if(ch1!=ch2)
   {
    flag=0;
    break;
   }
  }
  if(flag)
   cout<<"And the contents are also same...\n";
  else
   cout<<"But the contents are different...\n";
 }
 else if(s1>s2)
 {
  cout<<"\nThe size of "<<name<<" file is greater than "<<name1<<" file\n";
  cout<<"The size of "<<name<<" file is "<<s1<<endl;
  cout<<"The size of "<<name1<<" file is "<<s2<<endl;
 }
 else
 {
  cout<<"\nThe size of "<<name1<<" file is greater than "<<name<<" file\n";
  cout<<"The size of "<<name<<" file is "<<s1<<endl;
  cout<<"The size of "<<name1<<" file is "<<s2<<endl;
 }
 fin1.close();
 fin2.close();
}

void main()
{
 int ch;
 do
 {
  menu();
  cin>>ch;
  switch(ch)
  {
   case 1:writing(); break;
   case 2:reading(); break;
   case 3:rename(); break;
   case 4:remove(); break;
   case 5:copy();          break;
   case 6:countlines(); break;
   case 7:countwords(); break;
   case 8:searching(); break;
   case 9:replace(); break;
   case 10:vowels(); break;
   case 11:characters(); break;
   case 12:line();  break;
   case 13:append(); break;
   case 14:list();  break;
   case 15:compare(); break;
  }
  system("pause");
 }while(ch);
}




No comments

Post your comments

Powered by Blogger.