Write a program that reads a text file then displays this menu Display file Show
ID: 3708760 • Letter: W
Question
Write a program that reads a text file then displays this menu
Display file
Show statistics (word count, line count, letter frequency with table format)
Search
Save file with another name
Exit
Note 0: The program should read the file in a dedicated format; each line should not exceed 100 chars -max- (use cin.getline() ) and some of your own thought to try to figure this out. Make sure a word is not split in half
Note1: The Search results display the lines in the text file where the results where found followed by [n1,n2,.. ] where n is the column number where the result is (are)
Note 2: The save file option will save the file in the new format; each line will be a 100 chars long
in c++ the file is over 100 chars long
Explanation / Answer
Solution:
Note: Please don't forget to save the Test.txt file in the same folder/package where you are executung the code.
code:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string.h>
using namespace std;
ifstream infile;
//display file
void displayFile()
{
const int LINE_LENGTH = 100;
infile.open("Test.txt");
char line[LINE_LENGTH];
if (infile.is_open())
{
cout<<"File:"<<endl<<endl;
while (! infile.eof() )
{
infile.getline(line,LINE_LENGTH);//read line of 100 characters
cout<<line<<endl;
}
infile.close();
}
else
cout<<" File Not exists"<<endl;
}
// start of main program
void findWord()
{
int count=0;
char ch[20],c[20];
cout<<"Enter a word to count:";
cin>>c;
infile.open("Test.txt");
while(infile>>ch)//read word
{
if(strcmp(ch,c)==0)
count++;
}
cout<<"Occurrence of word ="<<count<<" times"<<endl;
// closes the file
infile.close();
}
void showStatistics()
{
// running the loop until file will end
char ch;
int countChar=1;
int countWords=1;
int countLines=1;
infile.open("Test.txt");
while(infile.get(ch))//read character
{
if(ch==' ')
{
countWords++;//count number of words
}
else if(ch==' '){
countWords++;//count number of words
countLines++;//count number of Lines
}
else{
countChar++;//count number of characters
}
}
// closes the file
infile.close();
_getch();
// display the number of character, words and line
cout<<" Lines words characters "<<endl;
cout<<countLines<<" "<<countWords<<" "<<countChar<<endl;
}
void saveFile()
{
infile.open("Test.txt");
const int LINE_LENGTH = 100;
char line[LINE_LENGTH];
ofstream outfile("Output.txt");
if (infile.is_open())
{
cout<<"File:"<<endl<<endl;
while (! infile.eof() )
{
infile.getline(line,LINE_LENGTH);//read line of 100 characters
outfile<<line<<endl;//print on other file
}
infile.close();
}
else
cout<<" File Not exists"<<endl;
}
int main()
{
// open the file for input.
ifstream infile("test.txt");
// create the stream in read-only mode
if(!infile) {
cout << "Cannot open file for reading. ";
_getch();
return 1;
}
// declares character and integer variables
char c;
int ch;
int f=true;
while(f)
{
cout<<"Menu: 1.Display file 2.Show statistics 3.Search 4.Save file 5.Exit "<<"Enter choice: ";
cin>>ch;//read choice
switch(ch)
{
case 1:
displayFile();
break;
case 2:
showStatistics();
break;
case 3:
findWord();
break;
case 4:
saveFile();
break;
case 5:f=false;
cout<<"Exiting...."<<endl;
break;
default:cout<<"Enter correct choice."<<endl;
}
}
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.