Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

AOA. any one know about Assignment 03 of cs201 plz share it

Views: 9695

Replies to This Discussion

which file name .............are you asking about the data.txt file ...if yes then jb program chalta ha just likh do    data.txt    ....lakin data.txt wali usi foldr me honi chaye jis me program ka code save ha ................

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

using namespace std;


int GenerateKey(string file){
int key = 0;
ifstream myfile();
//int val = 0;
if (myfileis())
{
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);
myfilemykey;
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;
}

}

}

coutendl;
system("pause"); 
}

. Here is a sample program in which we are using the simplest technique to generate the security key. You may copy and paste it into your Dev C++, and can see the results by entering any string. For example, We are Muslims.

#include <iostream>

#include <stdlib.h>

using namespace std;

main() {

                char data[1000];  // array to hold string entered from user

                int securitykey = 0; // variable to hold generated security key    

                cout"Input any string as data: ";

                cin.getline(data,1000, '\n'); // getline function allows to enter space between words while getting input from user.               

                for (int i = 0; data[i] != '\0'; i++){ // iterate through array unless reached at "null character"

                                securitykey += (int)data[i];  // converting each character in array to ASCII code and adding in securitykey

                }             

                cout"Security Key: "securitykey;  // printing security key on console

                system("pause");

}

The mechanism is so simple, ASCII code of each character is generated and added up into a variable named ‘securitykey’ using the statement:  securitykey += (int)data[i]; (int) has been converting the character into it's ASCII code.

 Note: 

         We have been using arrays to store characters; students are not bound to use the same technique. They might use any technique of their own for the desired output. 

Sloved assignmnt
#include
#include
using namespace std;
// this func will generate key bases on length of each line in file
int get_key(string a_lines)
{ int key = 0;
key += a_lines.length();
return key; }
// this func will find that file has key or not
bool findkey(string a_lines)
{ int ans = a_lines.find("%");
if(ans >= 0)
{ return 1; }
else
{ return 0; } }
bool readkey(string a_lines)
{

}
main()
{ ifstream infile; // input file handler
ofstream outfile;
char fileName[100]; // name of file to open
string line; // character array for each line in text file
string lines; // character array for full text file
int option;
cout "Enter the name of text file in current directory:"; cin >> fileName;
infile.open(fileName, ios::in); // opening file in input mode
bool jump = true;
bool ans;
if(!infile)
{ cout "file not found"; } else { while(!infile.eof()) { getline(infile, line); lines += line; } infile.close(); do { cout "\nOperations on text file"; cout "\n1.Protect my text file"; cout "\n2.check security of my text file"; cout "\n1.Enter your choice (1/2)"; cin >> option;
switch(option)
{ case 1:
if(findkey(lines))
{ cout "already protected"; cout " and return " findkey(lines); infile.close(); jump = false; break; } else { outfile.open(fileName, ios::app); outfile "%"; outfile get_key(lines); cout "key added"; cout "and return " findkey(lines); jump = false; outfile.close(); break; } case 2: if(findkey(lines)) { cout "file is protected using option 2"; cout "and return " findkey(lines); jump = false; break; } else { cout "file is not protected please protect file"; cout "and return " findkey(lines); } } } while(jump == true); } } //

ye program complete nai ha .....is me file contents are changed after protection alert ni arha 

salam to all guys mujy bi help chiye many sb error search kr k clear kr dea han bs ek problem ha mara key to_ string ni ho raha plz koi help kar dy plzzzzzzzzzzz
guys check this mara program error free ha lakin run bi ho raha ha likin sahi output ni da raha koi help kray ga plz.





#include
#include
#include
#include

using namespace std;


int GenerateKey(string file){
int key = 0;
ifstream myfile;

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("data.txt",ios::out | ios::app);
mykey += key;
myfilemykey;
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"\n Operations 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){

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("data.txt", 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;
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;
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 = "data.txt";
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("data.txt");
if(!myfile){
cout"\n Sorry the name of file "filename" you entered is not exist in current directory! ";
system("pause");
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;
}

}

}

coutendl;
system("pause");
}

program me ye line likho do.............myfile  mykey  key;

 blank spaces per less then less then sign aye ayega 

ho jaye ga (insha Allah)

bro kahan likhna hai myfile mykey key; ???

sign kaha ata hai?

phly data.txt file desktop kar save karni ha phir run ho ga program .

RSS

© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service