This assignment covers file I/O. Must design and code a program that can accompl
ID: 3797268 • Letter: T
Question
This assignment covers file I/O. Must design and code a program that can accomplish four things: copy one file to another (i.e., place the contents of file A into file B), ‘compress’ a file by removing all vowels (a, e, i, o, u, and y) from the file, merge two files, and finally double each line of an input file, saving into an output file. Also make it so the input file's content can be changed from the example given. Please send a test run as well. Please and thank you for your help. It's for C++
Program Specifications
The program should be menu driven with the top level menu being:
----------------------------------------------------------- Welcome to the File Copier
1) copy file A to file B
2) ‘compress’ file A (remove all vowels from the file), saving into file B
3) ‘double’ file A -- for each character in the input file, repeat it twice in the output file B.
4) ‘merge’ file A and file B saving into file C. (places all of file A in output file C, then adds all of file B after that into file C).
5) quit the program
-----------------------------------------------------------
Appropriate user prompts shall be provided for each choice (e.g.; for selection 2, you must prompt the user for the name of files A and B). Be sure to keep asking them what they want to do until they ask to quit the program. Be sure to check for errors.
For reversing, and compressing a file here is an example:
Input file:
This is a test. The lazy dog ran over the moon.
Able was I ere I saw elba.
I don't know what to type.
Output File (doubled):
TThhiiss iiss aa tteesstt.. TThhee llaazzyy ddoogg rraann oovveerr tthhee mmoooonn..
AAbbllee wwaass II eerree II ssaaww eellbbaa..
II ddoonn''tt kknnooww wwhhaatt ttoo ttyyppee..
Output File (compressed):
Ths s tst. Th lz dg rn vr th mn. bl ws r sw lb. dn't knw wht t tp.
Explanation / Answer
#include <iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<ctype.h>
#include<fstream.h>
void main( )
{
int choice, num, i ;
unsigned long int fact;
clrscr();
while (1)
{
cout<<"1. Copy File A to File B ";
cout<<"2. Compress file A (remove all vowels from the file), saving into file B ";
cout<<"3. 'Double’ file A -- for each character in the input file, repeat it twice in the output file B. ";
cout<<"4. ‘Merge’ file A and file B saving into file C "
cout<<"4. Exit ";
cout<<' Your choice ?";
cin>>choice ;
switch (choice)
{
case 1 :
ofstream outfile;
ifstream infile;
char fname1[10],fname2[20];
char ch,uch;
clrscr( );
cout<<"Enter a file name to be copied ";
cin>> fname1;
cout<<"Enter new file name";
cin>>fname2;
infile.open(fname1);
if( infile.fail( ) )
{
cerr<< " No such a file Exit";
getch();
exit(1);
}
outfile.open( fname2);
if(outfile.fail( ))
{
cerr<<"Unable to create a file";
getch();
exit(1);
}
while( !infile.eof( ) )
{
ch = (char) infile.get( );
outfile.put(ch);
}
infile.close( );
outfile.close( );
getch( );
break ;
case 2 :
ofstream outfile;
ifstream infile;
char fname1[10],fname2[20];
char ch,uch;
clrscr( );
cout<<"Enter a file name to be Compress ";
cin>> fname1;
cout<<"Enter new file name";
cin>>fname2;
infile.open(fname1);
if( infile.fail( ) )
{
cerr<< " No such a file Exit";
getch();
exit(1);
}
outfile.open( fname2);
if(outfile.fail( ))
{
cerr<<"Unable to create a file";
getch();
exit(1);
}
bool is_vowel(char ch){
static const auto vowels = { 'a', 'e', 'i', 'o', 'u' };
using namespace std;
return end(vowels) == find(begin(vowels), end(vowels),
static_const<char>( ::tolower(ch) );
}
auto c = char{ 0 };
std::noskipws(infile);
while(infile >> c){
if(is_vowel(c))
outfile << c;
}
break;
case 3 :
ofstream outfile;
ifstream infile;
char fname1[10],fname2[20];
char ch,uch;
clrscr( );
cout<<"Enter a file name to be copied ";
cin>> fname1;
cout<<"Enter new file name";
cin>>fname2;
infile.open(fname1);
if( infile.fail( ) )
{
cerr<< " No such a file Exit";
getch();
exit(1);
}
outfile.open( fname2);
if(outfile.fail( ))
{
cerr<<"Unable to create a file";
getch();
exit(1);
}
while( !infile.eof( ) )
{
ch = (char) infile.get( );
outfile.put(ch+ch);
}
infile.close( );
outfile.close( );
getch( );
break ;
case 4 :
ifstream ifiles1, ifiles2;
ofstream ifilet;
char ch, fname1[20], fname2[20], fname3[30];
cout<<"Enter first file name (with extension like file1.txt) : ";
gets(fname1);
cout<<"Enter second file name (with extension like file2.txt) : ";
gets(fname2);
cout<<"Enter name of file (with extension like file3.txt) which will store the contents of the two files (fname1 and fname1) : ";
gets(fname3);
ifiles1.open(fname1);
ifiles2.open(fname2);
if(ifiles1==NULL || ifiles2==NULL)
{
perror("Error Message ");
cout<<"Press any key to exit... ";
getch();
exit(EXIT_FAILURE);
}
ifilet.open(fname3);
if(!ifilet)
{
perror("Error Message ");
cout<<"Press any key to exit... ";
getch();
exit(EXIT_FAILURE);
}
while(ifiles1.eof()==0)
{
ifiles1>>ch;
ifilet<<ch;
}
while(ifiles2.eof()==0)
{
ifiles2>>ch;
ifilet<<ch;
}
cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
ifiles1.close();
ifiles2.close();
ifilet.close();
getch();
break;
case 5:
exit();
}
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.