Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <cstdlib>
- #include <iomanip>
- #include <string>
- using namespace std;
- int main()
- {
- ifstream input;
- ofstream output;
- string inputFileName;
- string outputFileName;
- char next;
- cout << "This program takes a file containing unformated javascript text, formats it, \n"
- << "and places it back into a new file. Make sure the source file is in the \nsame directory as the program.\n\n";
- cout << "What is the name of the source code file (max 20 characters): ";
- cin >> inputFileName;
- input.open(inputFileName);
- if (input.fail())
- {
- input.close();
- cout << "Error: failed to open '" << inputFileName << "'.\n\n";
- cout << "Press 'return' to end the program...";
- cin.get();
- exit(1);
- }
- cout << "What is the name of the output file (max 20 characters): ";
- cin >> outputFileName;
- output.open(outputFileName);
- if (output.fail())
- {
- cout << "Error: failed to open '" << outputFileName << "'.";
- cout << "Press 'return' to end the program...";
- cin.get();
- input.close();
- output.close();
- exit(1);
- }
- int spaceCount = 0;
- while (input >> next)
- {
- if (next == ';')
- output << next << endl << setw(spaceCount + 1);
- else if (next == '{')
- {
- output << endl << setw(spaceCount + 1) << next << endl;
- output.width(spaceCount += 4);
- }
- else if (next == '}')
- {
- output.width(spaceCount -= 4);
- output << endl << setw(spaceCount + 1) << next << endl;
- }
- else
- output << next;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement