Download Solution:
Please note that graded assignment no. 3 of CS201 has been uploaded. Due date to upload solutions is 10-07-2015 (Friday 10-July-2015).
All the instructions to solve the assignment are there in assignment file, so make sure that you read all these instructions and upload your solutions before due date.
Note:
You need to install Winrar to extract compressed assignment file. You can download Winrar from the following link http://www.rarlab.com/download.htm
For any query related to assignment, contact at: CS201@vu.edu.pk
--
Regards,
Instructor CS201
Solution guidelines
The program should operate in following sequence.
- Program will prompt the user to enter the file name in current directory.
- System will show a menu with two options:
- Protect the file
- Check security status
- In case user selects the first option (i.e. protect the file), the system will perform the following steps:
- Read the file
- Check if the file is already protected or not.
- In case if file is not protected, program will protect it by the protection mechanism defined above.
- If file is already protected, system will display appropriate message and exit.
- If user selects the second option (i.e. check security status), the system will perform following steps:
- Read the entire file and if key does not exist (i.e. file is not already protected), program will display the message to protect the file first and display menu.
- If file is already protected, the system will read the contents along with the already existing key. On the basis of contents read, a new key will be generated. If this new key and the existing one (read from the file) do not match, it means that the contents of protected file are changed and vice versa.
Important note
A text file (data.txt) is attached with assignment, use it for protection checking. Video file (Output-Demo.mp4) of desired function is also attached with assignment. Watch the demo video carefully to understand the required functionality (output) of program. Your program should work as per functionality performing in demo video file. You do not need to send text file with your solution. Just upload .cpp file from your LMS account. For any query related to assignment no. 3 contact us at cs201@vu.edu.pk.
Submission
You are required to submit your solution C++ program through LMS i.e. .cpp file
Download Solution:
Solution:
#include <iostream> #include <string> #include <cstring> #include <fstream> using namespace std; int GenerateKey(string file){ int key = 0; ifstream myfile(file); //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"); }
My dev version is 5.6.3. if any Query ask in Comments
tools ma jao....then compiler option ma jao...then
ReplyDeletesetting wali tab pe click karo then code generation ma jao then language standarad (-std) wali jaga pe iso c++11 standard choose karo aur ok kar do hope fully chal jai ga...ye mery compiler ki settings hain...ma ne c++11 standard rakha hua ha jo k by default ni hota karna parta ha
Thank you Zohaib bhai it worked.lakin VU walo ko kese pata hoga k hum ne konsa standerd user kiya ha .,..kiya hum is ko default wali setting se ni bna sakte
Deletevu waly is kism ki details ma ni jaty...aur na hi humain unko btany ki zaroorat ha...unho ne aik kaam dia tha hum ne kar dia....thats it...is standard se saari dunya wakif ha...ye c++11 standarad ha jo k 2011 ma aya tha...jo cheez standard k mutabik bani ho us ki tension ni lety....ager hum koi aur jesy boost ya phir koi doosri library use karty phr humain fikar mand hony ki zaroorat thi....kyo k wo vu k paas shayad na hoti tu ye waha pe chalni hi ni thi...
Delete