No tabs allowed! Objectives: Learn how to use the C++ file I/o, use character li
ID: 3906516 • Letter: N
Question
No tabs allowed! Objectives: Learn how to use the C++ file I/o, use character literals, and bool objects. Instructions Tabs can cause indentation problems vith programs. Your task is t replace tabs in a C++ source file. You should first prompt the user for an input file name and output file name. If the input file cannot be opened, then you should reprompt the user until the user supplies a file that can be opened. Likewise, a similar process should be performed for the output file. You should then copy the characters in the input file to the output file. Replace tabs not in comments, strings, or character literals with 3 spaces Replace tabs in strings or character 1iterals with the equivalent escape sequence (t) Tabs in comments should not be replaced. After the characters have been copied, you should indicate to the user the number?f tabs replaced Hints: Use bool variables to represent if the current character read from the input file is in a comment (//... or /* */), string ("..."),or character literal constant ('). Always save the previous character so you will know the last two characters that were read from the input file. Sample session: Enter an input file name: pgmi.cpp Enter an output file name pgm2.cpp The input file pgml.cpp was copied to pgm2.cpp with 8 tabs replaced Sample pgml.cpp input file (tabs are shown as ) int main) f // Tabs () in comments are not replaced. coutExplanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main(){
string name1,name2;
ifstream fin;
ofstream fout;
while(true){
cout << "Enter an input file name:";
cin >> name1;
fin.open(name1.c_str());
if (fin)
break;
else
cout << "Error opening file ";
}
while(true){
cout << "Enter an output file name:";
cin >> name2;
fout.open(name2.c_str());
if (fout)
break;
else
cout << "Error opening file ";
}
string line;
int count = 0;
while (getline(fin,line)){
for (int i = 0; i<line.length(); i++){
if (line[i] != ' '){
fout << line[i];
}
else {
fout << " ";
count++;
}
}
}
fin.close();
fout.close();
cout << "The input file " << name1 << " was copied to " << name2 << " with " << cout << " tabs replaces ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.