Problem Description:
(2) read into a C + + program, so that all the left curly braces "{" and the closing curly brace "}" in a single row, the new program is saved to another. cpp file, and the screen shows the processed program, displayed with the line number.
Code implementation:
#include <iostream> #include <cstdio> #include <cstring> #include <fstream> #include < Cstdlib>using namespace Std;int Main () {FStream myfile ("Source.cpp"); if (!myfile) {cerr<< "can ' t find source.cpp!\n"; Exit (1); } ofstream myfile2 ("Newsource.cpp"); if (!myfile2) {cerr<< "can ' t write newsource.cpp!\n"; Exit (1); } char t= ' + ', C; while (!myfile.eof ()) {myfile.get (c); if ((c== ' {' | | | c== '} ') && (t!= ' \ n ')) myfile2.put (' \ n '); else if ((t== ' {' | | | t== '} ') && (c!= ' \ n ')) myfile2.put (' \ n '); Myfile2.put (c); T=c; } myfile.close (); Myfile2.close (); cout<< "Work with been done!\n"; string S; cout<< "\nwatch the Newsource.cpp file!\n\n\n"; Ifstream in ("Newsource.cpp"); if (!in) {cerr<< "can ' t find newsource.cpp!\n"; Exit (1); } while (!in.eof ()) {getline (in,s); Cout<<s<< ' \12 '; } return 0;}
Operation Result:
"Project 4-programs to process C + + source code" (2)