Many Tasks related to Text File using Class n C++

//Create a class to display the menu with several file task operations such as count characters, digits, vowels, consonants, words, lines etc 

#include<fstream>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
using namespace std;

class FileOptions
{
char fileName[20];
fstream fio;
public:
void readFile();
void readFile(char[]);
void menu();
void displayFile();
void countVowels();
void countDigits();
void countCharacters();
void countConsonants();
void countSpecial();
void countWords();
void countLines();
void replaceWords();
void copyFile();
void removeFile();
void renameFile();
}f;

void FileOptions::menu()
{
cout<<"===================================\n";
cout<<".....MENU OF FILE TASK OPTIONS.....\n";
cout<<"===================================\n";
cout<<"0. Exit\n";
cout<<"1. Display File on screen\n";
cout<<"2. Count Vowels in File\n";
cout<<"3. Count Digits in File\n";
cout<<"4. Count All Characters in File\n";
cout<<"5. Count Consonants in File\n";
cout<<"6. Count Special Characters in File\n";
cout<<"7. Count Words in File\n";
cout<<"8. Count Lines in File\n";
cout<<"9. Find & Replace word in File\n";
cout<<"10. Copy text file\n";
cout<<"11. Remove text file\n";
cout<<"12. Rename text File\n";
cout<<"Enter your choice: ";
}

void FileOptions::readFile()
{
system("dir *.txt/w");
cout<<"Enter File Name: ";
cin>>fileName;
fio.open(fileName,ios::out|ios::in);
}

void FileOptions::readFile(char fn[])
{
strcpy(fileName,fn);
fio.open(fileName,ios::out|ios::in);
}

void FileOptions::displayFile()
{
readFile();
char ch;
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
ch=fio.get();
cout<<ch;
}
fio.close();
cout<<"\nFile contents displayed on screen...\n";
}
}

void FileOptions::countVowels()
{
readFile();
char ch;
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
ch=fio.get();
//cout<<ch;
switch(ch)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
count++;
break;
}
}
cout<<"There are "<<count<<" vowels in this file\n";
fio.close();
}
}

void FileOptions::countDigits()
{
readFile();
char ch;
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
ch=fio.get();
if(ch>='0' && ch<='9')
{
count++;
}
}
cout<<"There are "<<count<<" digits in this file\n";
fio.close();
}
}

void FileOptions::countCharacters()
{
readFile();
char ch;
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
ch=fio.get();
count++;
}
cout<<"There are total "<<count<<" characters in this file\n";
fio.close();
}
}

void FileOptions::countConsonants()
{
readFile();
char ch;
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
ch=fio.get();
if(isalpha(ch))
count++;
}
cout<<"There are total "<<count<<" consonants in this file\n";
fio.close();
}
}

void FileOptions::countSpecial()
{
readFile();
char ch;
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
ch=fio.get();
if(!(isalnum(ch) || isspace(ch)))
count++;
}
cout<<"There are total "<<count<<" special characters in this file\n";
fio.close();
}
}

void FileOptions::countWords()
{
readFile();
char wd[20];
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
fio.getline(wd,20,' ');
count++;
}
cout<<"There are total "<<count<<" words in this file\n";
fio.close();
}
}

void FileOptions::countLines()
{
readFile();
char line[80];
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
while(!fio.eof())
{
fio.getline(line,80,'\n');
count++;
}
cout<<"There are total "<<count<<" lines in this file\n";
fio.close();
}
}

void FileOptions::replaceWords()
{
readFile();
char wd[20], srch[20], nwrd[20];
int count=0;
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
ofstream fout("temp.txt",ios::out);
cout<<"Enter word you want to replace in file: ";
cin>>srch;
cout<<"Enter new word to be replaced in file : ";
cin>>nwrd;
while(!fio.eof())
{
fio.getline(wd,20,' ');
if(strcmpi(wd,srch)==0)
{
fout<<nwrd<<" ";
count++;
}
else
{
fout<<wd<<" ";
}
}
cout<<"Total "<<count<<" words has been replaced in this file\n";
fio.close();
fout.close();
remove(fileName);
rename("temp.txt",fileName);
}
}

void FileOptions::copyFile()
{
char oldFileName[20],newFileName[20], ch;
system("dir *.txt/w");
cout<<"Enter the filename to be copied: ";
cin>>oldFileName;
readFile(oldFileName);
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
cout<<"Enter the new name of file     : ";
cin>>newFileName;
ofstream fout(newFileName,ios::out);

while(!fio.eof())
{
ch=fio.get();
fout<<ch;
}
cout<<"All contents of file "<<oldFileName<<" has been copied to "
    <<newFileName<<endl; 
fio.close();
fout.close();
}
}

void FileOptions::removeFile()
{
char fName[20],cmd[20];
system("dir/w *.txt");
cout<<"Enter Filename to be deleted: ";
cin>>fName;
readFile(fName);
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
fio.close();
remove(fName);
cout<<"File "<<fName<<" is deleted successfully....\n";
}
}

void FileOptions::renameFile()
{
char fName[20],newFile[20];
system("dir/w *.txt");
cout<<"Enter Filename to be renamed: ";
cin>>fName;
readFile(fName);
if(!fio)
cout<<"No such file, check your entered file name....\n";
else
{
cout<<"Enter new name for File     : ";
cin>>newFile;
fio.close();
rename(fName,newFile);
cout<<"The file "<<fName<<" has been renamed as "<<newFile<<endl;
}
}

int main()
{
int ch;
char word[20];
do{
system("cls");
f.menu();
cin>>ch;
switch(ch)
{
case 1: f.displayFile(); break;
case 2: f.countVowels(); break;
case 3: f.countDigits(); break;
case 4: f.countCharacters(); break;
case 5: f.countConsonants(); break;
case 6: f.countSpecial(); break;
case 7: f.countWords(); break;
case 8: f.countLines(); break;
case 9: f.replaceWords(); break;
case 10: f.copyFile(); break;
case 11: f.removeFile(); break;
case 12: f.renameFile(); break;
}
system("pause");
}while(ch!=0);
cout<<"\n===================================\n";
return 0;
}




No comments

Post your comments

Powered by Blogger.