|
| 1 | +// FCI – Programming 1 – 2018 - Assignment 3 |
| 2 | +// Program Name: files.cpp |
| 3 | +// Last Modification Date: 28/03/2018 |
| 4 | +// Author1 and ID and Group: Ahmed Sayed Mansour Sayed 20170022 |
| 5 | +// Author2 and ID and Group: Abdulrahman Mohamed hassan 20170150 |
| 6 | +// Author3 and ID and Group: Osama Tareq Mohamed Saad 20170041 |
| 7 | +// Teaching Assistant: section 1's TA |
| 8 | +// Purpose:Text Processing |
| 9 | +#include <iostream> |
| 10 | +#include <fstream> |
| 11 | +#include <vector> |
| 12 | +#include <windows.h> |
| 13 | +#include <bits/stdc++.h> |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +void loadanexistingfile(char name[20], vector<char>& vec){ |
| 17 | + char ar; |
| 18 | + fstream lol; |
| 19 | + lol.open(name,ios::in); |
| 20 | + if(lol.fail()) |
| 21 | + cout<<"ERROR opening file !!\n"<<endl; |
| 22 | + else{ |
| 23 | + cout<<"file opened successfully.\n"; |
| 24 | + cout << "Now reading information.\n"; |
| 25 | + cout<<"data is loaded\n"<<endl; |
| 26 | + lol>>ar; |
| 27 | + while (lol.good() && !lol.eof()){ |
| 28 | + vec.push_back(ar); |
| 29 | + lol.get(ar); |
| 30 | + } |
| 31 | + } |
| 32 | + lol.close(); |
| 33 | +} |
| 34 | + |
| 35 | +void putvecinfile(char name[20],string name2,vector<char> vec){ |
| 36 | + fstream lol; |
| 37 | + loadanexistingfile(name , vec); |
| 38 | + cout <<"enter the name of the file you want save the data to : "; |
| 39 | + cin >>name2; |
| 40 | + lol.open(name2,ios::out); |
| 41 | + for(int i=0 ;i<vec.size();i++) |
| 42 | + lol.put(vec[i]); |
| 43 | + lol.close(); |
| 44 | +} |
| 45 | + |
| 46 | +void coutvictor(vector<char>& vec,char name[20]){ |
| 47 | + loadanexistingfile(name , vec); |
| 48 | + for (int i=0;i<vec.size();i++) |
| 49 | + cout<<vec[i]; |
| 50 | +} |
| 51 | + |
| 52 | +void putconinfile(string name2 ,string content){ |
| 53 | + fstream lol; |
| 54 | + lol.open(name2,ios::out); |
| 55 | + lol<<content; |
| 56 | + lol.close(); |
| 57 | + cout<<"File was created successfully\n "<<endl; |
| 58 | +} |
| 59 | + void counting (char name[20]){ |
| 60 | + char cha,line[20]; |
| 61 | + int character=0,word=0; |
| 62 | + fstream lol ; |
| 63 | + lol.open(name,ios::in); |
| 64 | + if (lol.is_open()){ |
| 65 | + cout << "file open successfully : \n"; |
| 66 | + while(lol>>line){ |
| 67 | + for(int i=0;i<20;i++){ |
| 68 | + line[i]=tolower(line[i]); |
| 69 | + if (int (line[i]== 0)) break; |
| 70 | + if (line[i]>='a'&&line[i]<='z') character++; |
| 71 | + } |
| 72 | + word++; |
| 73 | + } |
| 74 | + cout << "\nnumber of characters = "<<character << "\nnumber of words is = "<< word<<endl; |
| 75 | + } |
| 76 | + else cout << "file couldn't open "; |
| 77 | + lol.close(); |
| 78 | + } |
| 79 | + void searching(char name[20]){ |
| 80 | + char line[20],srch1[20]; |
| 81 | + bool found =0; |
| 82 | + fstream lol; |
| 83 | + vector <char> srch,srch2; |
| 84 | + cout <<"enter the name to search :"; |
| 85 | + cin>>srch1; |
| 86 | + for(int i=0;i<20;i++){ |
| 87 | + if (int (srch1[i]== 0)) break; |
| 88 | + srch.push_back(tolower(srch1[i])); |
| 89 | + } |
| 90 | + lol.open(name,ios::in); |
| 91 | + while (lol>> line){ |
| 92 | + for(int i=0;i<20;i++){ |
| 93 | + if (int (line[i]== 0)) break; |
| 94 | + srch2.push_back(tolower(line[i])); |
| 95 | + } |
| 96 | + if (srch == srch2) { |
| 97 | + found =1; |
| 98 | + break; |
| 99 | + } |
| 100 | + else { |
| 101 | + found=0; |
| 102 | + srch2.clear(); |
| 103 | + } |
| 104 | + } |
| 105 | + (found==1)?cout <<"\nWord was found in the file :)\n":cout <<"\nWord is not found in the file:(\n"; |
| 106 | + lol.close(); |
| 107 | + } |
| 108 | + |
| 109 | + void clearing(char name[20]){ |
| 110 | + fstream lol; |
| 111 | + lol.open(name , ios::out | ios::trunc); |
| 112 | + } |
| 113 | +string addword(string x,char name[20]) |
| 114 | +{ |
| 115 | + ofstream lol(name, ios::app); |
| 116 | + lol << " " << x; |
| 117 | + lol.close(); |
| 118 | + return x; |
| 119 | +} |
| 120 | + |
| 121 | +void replace(string x, string y,char name[20]) |
| 122 | +{ |
| 123 | + string word, allwords; |
| 124 | + ifstream lol(name); |
| 125 | + while (!lol.eof()) |
| 126 | + { |
| 127 | + lol >> word; |
| 128 | + if (x == word) |
| 129 | + { |
| 130 | + allwords = allwords + y; |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + allwords = allwords + " " + word + " "; |
| 135 | + } |
| 136 | + } |
| 137 | + lol.close(); |
| 138 | + ofstream lol1(name); |
| 139 | + lol1 << allwords; |
| 140 | + |
| 141 | +} |
| 142 | +char upper(char name[20]) |
| 143 | +{ |
| 144 | + |
| 145 | + char arr[100]; |
| 146 | + ifstream lol(name); |
| 147 | + lol.getline(arr,100,EOF); |
| 148 | + lol.close(); |
| 149 | + ofstream lol1(name,ios::out); |
| 150 | + for (int i=0;i<strlen(arr);i++) |
| 151 | + { |
| 152 | + if (arr[i] >= 'a' && arr[i] <= 'z') |
| 153 | + { |
| 154 | + arr[i] = (char)(arr[i] - 32); |
| 155 | + } |
| 156 | + } |
| 157 | + for (int i = 0; i < strlen(arr); i++) |
| 158 | + { |
| 159 | + lol1 << arr[i]; |
| 160 | + } |
| 161 | + lol1.close(); |
| 162 | +} |
| 163 | +char lower(char name[20]) |
| 164 | +{ |
| 165 | + char arr[100]; |
| 166 | + ifstream lol(name); |
| 167 | + lol.getline(arr,100,EOF); |
| 168 | + lol.close(); |
| 169 | + ofstream lol1(name,ios::out); |
| 170 | + for (int i=0;i<strlen(arr);i++) |
| 171 | + { |
| 172 | + if (arr[i] >= 'A' && arr[i] <= 'Z') |
| 173 | + { |
| 174 | + arr[i] = (char)(arr[i] + 32); |
| 175 | + } |
| 176 | + } |
| 177 | + for (int i = 0; i < strlen(arr); i++) |
| 178 | + { |
| 179 | + lol1 << arr[i]; |
| 180 | + } |
| 181 | + lol1.close(); |
| 182 | +} |
| 183 | +void cipherrot13(vector<char> &vec ,char name[20]){ |
| 184 | + loadanexistingfile(name , vec); |
| 185 | + char test; |
| 186 | + int index; |
| 187 | + fstream lol; |
| 188 | + lol.open(name,ios::out|ios::trunc); |
| 189 | + for(int i=0;i<vec.size();i++){ |
| 190 | + test=vec[i]; |
| 191 | + if (test >= 'A' && test<='Z'){ |
| 192 | + index=test + 13; |
| 193 | + if (index>90) |
| 194 | + index -= 26; |
| 195 | + vec[i] = (char)index;} |
| 196 | + else if (test >= 'a' && test<='z') { |
| 197 | + index=test + 13; |
| 198 | + if (index>122) |
| 199 | + index -= 26; |
| 200 | + vec[i] = (char)index;}} |
| 201 | + coutvictor(vec,name); |
| 202 | + for(int i=0 ;i<vec.size();i++) |
| 203 | + lol.put(vec[i]); |
| 204 | + lol.close(); |
| 205 | +} |
| 206 | +int main() |
| 207 | +{ |
| 208 | + while (true){ |
| 209 | + int character1=0,word1=0,cipherornot; |
| 210 | + bool found1=0; |
| 211 | + char line[20],cha,ar,name[20]; |
| 212 | + string content,name2,choice; |
| 213 | + vector<char> vec,vec2; |
| 214 | + cout <<"Ahlan ya user ya hbibi"<< "\nWhat do you like to do today?"<<"\n1- Load an existing file"<<"\n2- Create a new file"<< |
| 215 | + "\n3- Display file content"<<"\n4- Save the loaded text to the same file again or different one"<< |
| 216 | + "\n5- Count the words and the characters in the file "<<"\n6- Search for a word in a file "<<"\n7- Empty file content "<< |
| 217 | + "\n8- Add more content to the end of the file"<<"\n9- Replace a word in the file with another word"<< |
| 218 | + "\n10- Turn all the content file to upper case"<<"\n11- Turn all the content file to lower case"<< |
| 219 | + "\n12- Add another file to the end of the current file"<<"\n13- Encrypt and decrypt the file"<<"\nE- End\n"; |
| 220 | + cout <<"enter your choice : "; |
| 221 | + cin >> choice ; |
| 222 | + if(choice=="E" || choice=="e") return 0; |
| 223 | + else if (choice=="1"){ |
| 224 | + cout <<"enter the file name: " ; |
| 225 | + cin >>name; |
| 226 | + loadanexistingfile(name,vec); |
| 227 | + } |
| 228 | + else if (choice=="2"){ |
| 229 | + cout <<"enter the file name: " ; |
| 230 | + cin.ignore(); |
| 231 | + getline(cin,name2); |
| 232 | + cout<<"Put content in created file "<<name<<": "; |
| 233 | + getline(cin,content); |
| 234 | + if(isspace(content.back())) content.erase(content.begin()+(content.length()-1)); |
| 235 | + putconinfile(name2,content);} |
| 236 | + else if (choice=="3"){ |
| 237 | + cout<<"loading data from loaded file \n"<<endl; |
| 238 | + coutvictor(vec,name); |
| 239 | + cout<<endl<<endl; |
| 240 | + } |
| 241 | + else if(choice=="4"){ |
| 242 | + putvecinfile(name,name2,vec); |
| 243 | + cout<<"file saved successfully\n"<<endl;} |
| 244 | + else if (choice == "5") counting(name); |
| 245 | + else if (choice == "6") searching(name); |
| 246 | + else if (choice == "7") clearing(name); |
| 247 | + else if (choice == "8") { |
| 248 | + string word; |
| 249 | + cout << "Enter your word:"; |
| 250 | + cin.ignore(); |
| 251 | + getline(cin, word); |
| 252 | + addword(word,name); |
| 253 | + } |
| 254 | + else if (choice == "9") { |
| 255 | + string oldword, newword; |
| 256 | + cout << "Enter the word which in the file:"; |
| 257 | + cin >> oldword; |
| 258 | + cout << "Enter the new word:"; |
| 259 | + cin >> newword; |
| 260 | + replace(oldword,newword,name); |
| 261 | + } |
| 262 | + else if(choice=="10") upper(name); |
| 263 | + else if(choice=="11") lower(name); |
| 264 | + else if(choice=="12"){ |
| 265 | + fstream lol; |
| 266 | + loadanexistingfile(name,vec); |
| 267 | + cout<<"enter the name of the file you want to copy data from : "; |
| 268 | + cin.ignore(); |
| 269 | + getline(cin,name2); |
| 270 | + lol.open(name2,ios::in); |
| 271 | + if(lol.fail()) |
| 272 | + cout<<"ERROR opening file !!\n"<<endl; |
| 273 | + else{ |
| 274 | + vec.push_back('\n'); |
| 275 | + while(lol.get(ar)){ |
| 276 | + vec.push_back(ar); |
| 277 | + } |
| 278 | + lol.close(); |
| 279 | + cout<<"the new file content is :"<<endl; |
| 280 | + lol.open(name,ios::out | ios::trunc); |
| 281 | + for (int i=0;i<vec.size();i++){ |
| 282 | + cout <<vec[i]; |
| 283 | + lol.put(vec[i]); |
| 284 | + } |
| 285 | + } |
| 286 | + } |
| 287 | + else if(choice=="13"){ |
| 288 | + cout<<"choose :\n1-Cipher.\n2-Decipher\n"; |
| 289 | + cin>>cipherornot; |
| 290 | + if(cipherornot==1){ |
| 291 | + cipherrot13(vec,name);} |
| 292 | + else if(cipherornot==2){ |
| 293 | + cipherrot13(vec,name);} |
| 294 | + cout <<endl; |
| 295 | + } |
| 296 | + cout <<endl; |
| 297 | + Sleep(600); |
| 298 | + } |
| 299 | +} |
0 commit comments