Assigning key to txt file in console randomly, and checking the txt file after editing.

This line give error, any alternative for this???
"myfile.open(filename,ios::out |ios::app);"
work fine in "isoc++11" but didn't work without it.


#include <iostream>
#include <string>
#include <cstring>
#include <fstream>

using namespace std;


int GenerateKey(string file){
int key = 0;
ifstream myfile(myfile);
//int val = 0;
if (myfile.is_open())
{
string line = "";
while (getline(myfile, line))
{
key += line.length();
}
return key;
}
}
bool findKey(string);

int ProtectFile(string filename){
int key = GenerateKey(filename);
string mykey = "%";
ofstream myfile;
myfile.open(filename,ios::out |ios::app);
mykey += to_string(key);
myfile<<mykey;
myfile.close();
return key;
}
int getfileLength(string);
int GetKey(string);
int displayMenu(string filename){
bool check = false;
int key = 0;
int msg = 1;
check = findKey(filename);
int getval = 0;
cout<<"\nOperations on text file\n";
cout<<"1. Protect my text file\n";
cout<<"2. Check Security of my text file\n\n";
cout<<"Enter your choice (1/2): ";
cin>>getval;
if(check == false && getval == 2){
msg = 1;
}else if(check == false && getval == 1){

key = ProtectFile(filename);
msg = 2;
}
else if(check == true && getval == 1){
//checkForChange();
msg = 3;
}
else if(check == true && getval ==2){
if( GetKey(filename)==getfileLength(filename)){
msg = 4;
}
else if(getfileLength(filename)!=GetKey(filename)){
msg = 5;
}

}
return msg;

}
bool findKey(string filename){
bool check = false;
string key = "%";
string line = "";
ifstream myfile;
myfile.open(filename, ios::in);
size_t pos;
while(myfile.good())
{
getline(myfile,line);
pos=line.find(key);
if(pos!=string::npos)
{
check = true;
break;
}
}
return check;
}
int getfileLength(string filename){
ifstream myfile (filename);
int val = 0;
int key = 0;string line;
if (myfile.is_open())
{
while (getline(myfile, line))
{
size_t pos = line.find('%');
if (pos != string::npos)
{
line = line.substr(0,pos);
}
val += line.length();
}

}
myfile.close();
return val;
}
int GetKey(string filename){
ifstream myfile (filename);
int val = 0;
int key = 0;string line;
if (myfile.is_open())
{

while (getline(myfile, line))
{
size_t pos = line.find('%');
if (pos != string::npos)
{
line = line.substr(pos+1);
}
}
}
myfile.close();
return atoi(line.c_str());
}
int main(){
string filename = "";
int key = 0;
bool choice = false;
int msg = 1;
ifstream myfile;
cout<<"Enter the name of text file in current directory: ";
cin>>filename;

myfile.open(filename);
if(!myfile){
cout<<"\nSorry the name of file "<<filename<<" you entered is not exist in current directory!";
return 0;
}else{
while(msg == 1){
msg = displayMenu(filename);
if(msg == 1)
cout<<"Your text file is not protected, first protect it by using option \"1\"\n\n";
if(msg == 2){
cout<<"\nYour text file is protected now!";
break;
}
else if(msg == 3){
cout<<"\nYour text file is already protected!";
break;
}
else if(msg == 4){
cout<<"The content of file are not changed after protection.";
break;

}else if(msg == 5){
cout<<"Alert! The content of file are changed after protection.";
break;
}

}

}

cout<<endl;
system("pause");
}
Last edited on
Before C++11, file streams can't be opened with std::string filenames. You'll have to use c_str() to get a C-style string.
Topic archived. No new replies allowed.