Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: Learn how to use the C++ file I/O, use character literals, and bool

ID: 3904773 • Letter: O

Question

Objectives: Learn how to use the C++ file I/O, use character literals, and bool objects.

Instructions: Tabs can cause indentation problems with programs. Your task is to 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 literals with the equivalent escape sequence ( ). Tabs in comments should not be replaced. After the characters have been copied, you should indicate to the user the number of 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: pgm1.cpp

Enter an output file name: pgm2.cpp

The input file pgm1.cpp was copied to pgm2.cpp with 8 tabs replaced.

Sample pgm1.cppinput file (tabs are shown as <tab>):

int main()

{

<tab>// Tabs (<tab>) in comments are not replaced.

<tab>cout << "Tabs (<tab>) in strings are replaced." << endl;

<tab>if (a == <tab>0)

<tab><tab>a = 1;

}

Sample pgm2.cppoutput file (tabs are shown as <tab>):

int main()

{

// Tabs(<tab>) in comments are not replaced

cout << "Tabs ( ) in strings are replaced. " << end;

if (a == 0) a = 1;

return 0;

}

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main(){

   ifstream fin;
   ofstream fout;
   string name1,name2;

   while(true){
      cout << "Enter input file name:";
      cin >> name1;
      fin.open((char *)name1.c_str());
      if (!fin){
         cout << "Error opening file ";
      }
      else
        break;
   }
   while(true){
      cout << "Enter output file name:";
      cin >> name2;
      fout.open((char *)name2.c_str());
      if (!fin){
         cout << "Error opening file ";
      }
      else
        break;
   }
   int count = 0;
   int commenton = 0;
   string line;
   while(getline(fin,line)){
       if (line[0] == '/' && line[1] == '/')
          fout << line << endl;
       else if (line[0] == '/' && line[1] == '*'){
               fout << line << endl;
               if (line[line.length()-2] != '*' && line[1] != '/'){
                   commenton = 1;
               }
       }
       else if (commenton == 1 && line[line.length()-2] == '*' && line[1] == '/'){
            fout << line << endl;
            commenton = 0;
       }
       if (commenton == 0){
            for (int i = 0; i <line.length(); i++){
               if (line[i] != ' ')
                  fout << line[i];
               else {
                  fout << "   ";
                  count++;
               }
            }
       }
    
   }
   cout << "The input file " << name1 << " was copied to " << name2 << " with " << count << " tabs replaced." << endl;
   return 0;
}