/*creat a text file and output to the file, while quite on keyword "Quite"*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
#define COL_WIDTH 80
usingnamespace std;
bool filecmp(char *x, char *Q);
int main (){
// user input keyword to quite
char Qt[6] = "Quite";
char filename[MAX_PATH + 1];
char input_line[COL_WIDTH + 1];
cout << "Enter a file name : " ;
cin.getline(filename , MAX_PATH);
ofstream file(filename);
if (file == NULL){
cout << "The file cannot be created "<< endl;
return -1;
system("PAUSE");
}
cout << filename << " is opened/created .\n Enter your sentence below or enter \"Quite\" to quite " << endl;
while (true ){
cin.getline(input_line , COL_WIDTH );
if (filecmp(input_line,Qt) == true )
break;
file << input_line;
}
}
bool filecmp(char *x, char *Q){
cout << "\t<FILE_CMP reached>\n";
//problem is here :
if (strlen(x)!=strlen(Q)){
//compare length of the two char and return false if !=
returnfalse;
}
//----------------------
for (int i = 0 ; i < 6 ; i++){
cout << "\t< FILE_CMP > <" <<i << "> :\n";
if (x[i]!=Q[i]){
// the below three lines are tests, return false if the String doesn't match.
cout << "\tFILE_CMP returned <false>.\n";
cout << "\tx[i] is : "<<x[i]<<endl;
cout << "\tQ[i] is : "<<Q[i]<<endl;
returnfalse ;
}
}
cout << "\tFILE_CMP returned <true>.\n";
returntrue;
}