1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
void heading();
void validateInputFile(ifstream&, string&);
void createOutputFile(fstream&, string);
void makeCopy(ifstream&, fstream&, string, string);
void displayInputFile(ifstream&);
void displayOutputFile(fstream&);
int main()
{
string inFile;
string outFile;
ifstream inputFile;
fstream outputFile;
heading();
cout << "\n\nEnter an input file name: " ;
cin >> inFile;
validateInputFile(inputFile, inFile);
cout << "\nEnter an output file name: ";
cin >> outFile;
createOutputFile(outputFile, outFile);
cout << "\n------------------------------------------------------------" << endl;
makeCopy(inputFile, outputFile, inFile, outFile);
outputFile.close();
inputFile.close();
inputFile.open(inFile.c_str());
outputFile.open(outFile.c_str(), ios::in);
cout << "\n------------------------------------------------------------" << endl;
displayInputFile(inputFile);
cout << "\n------------------------------------------------------------" << endl;
displayOutputFile(outputFile);
inputFile.close();
outputFile.close();
return 0;
}
void heading(){
cout << "************************************************************" << endl;
cout << "|" << setw(59) << "|" << endl;
cout << "|" << setw(59) << "|" << endl;
cout << "|" << setw(43) << "CMPS 400: Operating Systems" << setw(16) << "|" << endl;
cout << "|" << setw(59) << "|" << endl;
cout << "| Designed by: Brittany" << setw(33) << "|" << endl;
cout << "|" << setw(59) << "|" << endl;
cout << "| Program Objective:" << setw(40) << "|" << endl;
cout << "| This program will validate an input file, create an" << setw(6) << "|" << endl;
cout << "| output file, and copy the contents from the input" << setw(8) << "|" <<endl;
cout << "| file to the output file." << setw(33) << "|" << endl;
cout << "|" << setw(59) << "|" << endl;
cout << "|" << setw(59) << "|" << endl;
cout << "************************************************************" << endl;
}
void validateInputFile(ifstream &inputFile, string &file){
int count=0;
inputFile.open(file.c_str());
count++;
while (inputFile.fail()) {
cout << "The file does not exist." << endl;
if(count==3){
cout << "You have tried " << count << " times. Try running the program again." << endl;
exit(0);
}
cout << "Please enter another filename: " << endl;
cin >> file;
inputFile.open(file.c_str());
count++;
}
cout << "The file \"" << file << "\" exists." << endl;
}
void createOutputFile(fstream &outputFile, string file){
outputFile.open(file.c_str(), ios::out);
}
void makeCopy(ifstream &input, fstream &output, string in, string out){
cout << "\nThe file \"" << in << "\" is being copied to the file \"" << out << "\"." << endl;
int count = 1;
string line;
getline(input, line, '\n');
while (input) {
output << count << line << endl;
count++;
getline(input, line, '\n');
}
}
void displayInputFile(ifstream &input){
cout << "\nThis is the contents of the input file:\n" << endl;
string line;
getline(input, line, '\n');
while (input) {
cout << line << endl;
getline(input, line, '\n');
}
}
void displayOutputFile(fstream &output){
cout << "\nThis is the contents of the output file:\n" << endl;
string line;
getline(output, line, '\n');
while (output) {
cout << line << endl;
getline(output, line, '\n');
}
}
| |