Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     ifstream input;
  11.     ofstream output;
  12.     string inputFileName;
  13.     string outputFileName;
  14.     char next;
  15.  
  16.     cout << "This program takes a file containing unformated javascript text, formats it, \n"
  17.          << "and places it back into a new file. Make sure the source file is in the \nsame directory as the program.\n\n";
  18.    
  19.     cout << "What is the name of the source code file (max 20 characters): ";
  20.     cin  >> inputFileName;
  21.    
  22.     input.open(inputFileName);
  23.     if (input.fail())
  24.     {
  25.         input.close();
  26.         cout << "Error: failed to open '" << inputFileName << "'.\n\n";
  27.         cout << "Press 'return' to end the program...";
  28.         cin.get();
  29.         exit(1);
  30.     }
  31.  
  32.     cout << "What is the name of the output file (max 20 characters): ";
  33.     cin  >> outputFileName;
  34.  
  35.     output.open(outputFileName);
  36.     if (output.fail())
  37.     {
  38.         cout << "Error: failed to open '" << outputFileName << "'.";
  39.         cout << "Press 'return' to end the program...";
  40.         cin.get();
  41.         input.close();
  42.         output.close();
  43.         exit(1);
  44.     }
  45.  
  46.     int spaceCount = 0;
  47.     while (input >> next)
  48.     {
  49.         if (next == ';')
  50.             output << next << endl << setw(spaceCount + 1);
  51.         else if (next == '{')
  52.         {
  53.             output << endl << setw(spaceCount + 1) << next << endl;
  54.             output.width(spaceCount += 4);
  55.         }
  56.         else if (next == '}')
  57.         {
  58.             output.width(spaceCount -= 4);
  59.             output << endl << setw(spaceCount + 1) << next << endl;
  60.         }
  61.         else
  62.             output << next;
  63.     }
  64.  
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement