Need help changing the data in a file and writing it back to a new one. Open the
ID: 3704345 • Letter: N
Question
Need help changing the data in a file and writing it back to a new one.
Open the file “cad.dat”. (This was something I will create so it can be any text)
Calculate and report to the screen the number of upper case and lower case characters.
Read all of the characters in that file and convert each of characters as follows:
Substitute every whitespace with a “-”
Substitute every digit with a “#”
Substitute every upper case letter with a corresponding lower case letter.
Substitute every lower case letter with a corresponding upper case letter.
Write the converted characters to the file “outputs.dat”.
I have this framework already and need to use it in my program:
Explanation / Answer
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void addPlusPlus(ifstream& inStream, ofstream& outStream);
int main( )
{
ifstream fin;
ofstream fout;
cout << "Begin editing files. ";
fin.open("cad.dat");
if (fin.fail( ))
{
cout << "Input file opening failed. ";
exit(1);
}
fout.open("output.dat");
if (fout.fail( ))
{
cout << "Output file opening failed. ";
exit(1);
}
addPlusPlus(fin, fout);
fin.close( );
fout.close( );
cout << "End of editing files. ";
return 0;
}
void addPlusPlus(ifstream& inStream, ofstream& outStream)
{
char next;
inStream.get(next);
while (! inStream.eof( ))
{
if (isspace(next))
outStream << "-";
else if (isdigit(next))
outStream << "#";
else if ((next >= 'A') && (next <= 'Z')) {
next = next + 32;
outStream << next;
}
else if ((next >= 'a') && (next <= 'z')) {
next = next - 32;
outStream << next;
}
inStream.get(next);
}
}
/*************** Output of Program *****************
cad.dat file contents
-------------------------------------------------------------------
helloTHERE , how are you Today. Today is 12th of January.
thanks
output.dat
-------------------------------------------------------------------
HELLOthere------HOW-ARE-YOU-tODAY-tODAY-IS-##TH-OF-jANUARY-THANKS-
***************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.