Write a program that will prompt for a template file name, a substitution string
ID: 3669695 • Letter: W
Question
Write a program that will prompt for a template file name, a substitution string, and an output file name.
Your program will: 1. Open the template file for input. This template file will have line of inputs consist of the symbol ‘*’ or ' ' (space).
Input file: test.dat
*********
*** * ***
*********
2. Process each line in the file by replacing each ‘*’ character with the letters of the substitution string and output to an output file.
For example: Substitution string: HW6
Output file: test.txt
HW6HW6HW6
HW6 H W6H
HW6HW6HW6
Note that the substitution string is reset to the first character after each line.
Explanation / Answer
Here is the code for you.....
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
string inputFile, subStr, outputFile;
cout<<"Enter the template file name: ";
cin>>inputFile;
cout<<"Enter the substitution string: ";
cin>>subStr;
cout<<"Enter the output file name: ";
cin>>outputFile;
ifstream inputFilePtr;
inputFilePtr.open(inputFile);
ofstream outputFilePtr;
outputFilePtr.open(outputFile);
int count = 0;
int len = subStr.length();
while(!inputFilePtr.eof())
{
char x;
inputFilePtr >> noskipws >> x;
if(x == ' ')
{
count = 0;
outputFilePtr << ' ';
}
if(x == '*')
outputFilePtr << subStr.at(count % len);
else
outputFilePtr << ' ';
count++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.