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: 9713

Replies to This Discussion

data.txt file ap nay wahan save krni hai jahan ap apny program save kr rhi hain... wrna complete path dain program run jb ho us may file ka

yeah right 

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
char fileName[500];
ifstream fileReader;
ofstream fileWriter;
int choice;

cout "Enter the name of text file in current directory:";
cin >> fileName;
fileReader.open(fileName, ios::in);
if(!fileReader)
{
cout "Invalid File Name";
return 0;

}
cout "Operations on text file" endl;
cout "1. Protect my text file" endl;
cout "2. check security of my text file" endl endl;
cout "1.Enter your choice (1/2): ";
cin >> choice;

string oneLine;
string allLines;

while(!fileReader.eof())
{
getline(fileReader, oneLine);
allLines += oneLine;
}

int find1 = allLines.find("%");
switch (choice)
{
case 1:
if(find1>0)
{
cout "Your text file is already protected";
system("pause");
fileReader.close();
break;
}
else
{
int key = 0;
key += allLines.length();
fileWriter.open(fileName, ios::app);
fileWriter "%" key;
cout "Your Text file is protected now.";
fileWriter.close();
break;
}
case 2:
if(find1<=0)
{
cout "Your text file is not protected. First protect the file by using option 1";
fileReader.close();
break;
}
else
{
int key = 0;
key += allLines.length();
key = key-4;
stringstream convert;
convert key;
string sKey = convert.str();
int find2 = allLines.find(sKey);
if(find2>0)
{
cout "The Contents of file are not changed after protection";
fileReader.close();
break;
}
else
{
cout "Alert! The Contents of file are changed after protection";
fileReader.close();
break;
}
}
}
system("pause");
}

enjoy guys working solution

100 percent working solution hai guys

code is not working ...............

fileWriter "%" key;

convert key

data text fie ka hota h

pz koi bte ga

After joining VU first assignment with poor solution .

in straight word aik b lafz samj nae aya.

#include <iostream>
#include <fstream>
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); } }


//

clude
#include
using namespace std;

int get_key(string a_lines)
{ int key = 0;
key += a_lines.length();
return key; }

bool findkey(string a_lines)
{ int answer = a_lines.find("%");
if(answer >= 0)
{ return 1; }
else
{ return 0; } }
bool readkey(string a_lines)
{

}
main()

{ ifstream infile;
ofstream outfile;
char fName[100];
string line;
string lines;
int option;
cout "Enter the name of text file in current directory:";
cin >> fName;
infile.open(fName, ios::in); // opening file in input mode
bool jump = true;
bool answer;
if(!infile)
{ cout "file not found choose 1"; }
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(fName, 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);
jump=false;
break; } } }



while(jump == true);
system("pause");
} }

#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"); 
}

Having some bugs in this code.

RSS

Looking For Something? Search Below

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

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