-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram-file
More file actions
269 lines (262 loc) · 8.21 KB
/
program-file
File metadata and controls
269 lines (262 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <bits/stdc++.h>
#include<fstream>
using namespace std;
string LoadFile();
void CreateFile();
void Display(string name);
void Save(string name);
int CountWords(string name);
int countChar(string name);
void FindWord(string name);
void Empty(string name);
void input();
void ReplaceWord(string name);
void UpperCase();
void LowerCase();
void AddFile();
void Cipher(string name);
int main()
{
string name;
int number;
cout << "Ahlan ya user ya hbibi. \nWhat do you like to do today? \n\n";
while(true){
cout <<"1- Load an existing file \n2- Create a new file \n3- Display file content \n4- Save the loaded text to the same file again or different one \n5- Count the words and the characters in the file\n";
cout << "6- Search for a word in a file \n7- Empty file content \n8- Add more content to the end of the file \n9- Replace a word in the file with another word \n10- Turn all the content file to upper case\n";
cout <<"11- Turn all the content file to lower case \n12- Add another file to the end of the current file \n13- Encrypt and decrypt the file \n14- End\n\n";
int choice;
cout << "Enter your choice : ";
cin >> choice;
while(choice > 14 || choice<1) cin >> choice;
if (choice==1) name =LoadFile();
else if(choice==2) CreateFile();
else if(choice==3) Display(name);
else if(choice==4) Save(name);
else if(choice==5){ cout << "Number of words = " << CountWords(name) << endl;
cout << "Number of Characters = " << countChar(name) << endl << endl;}
else if(choice==6) FindWord(name);
else if(choice==7) Empty(name);
else if(choice==8) input();
else if(choice==9) ReplaceWord(name);
else if(choice==10) UpperCase();
else if(choice==11) LowerCase();
else if(choice==12) AddFile();
else if(choice==13) Cipher(name);
else if(choice==14){ cout << "\nKher ma 3mlt walahy." << endl << endl;
break;}
}
cout << endl;
return 0;
}
string LoadFile(){
fstream file;
char name[81];
cout << "Enter the file name :";
cin >> name;
file.open(name,ios::in);
if(file.fail()) cout << "\nError!. file not exist\n";
else cout << "\nThe file loaded successfully.\n\n";
return name;
}
void Display(string name){
fstream file;
char text;
file.open(name,ios::in);
cout << endl ;
while(!file.eof() && !file.fail()){
file.get(text);
cout << text;
}
cout << endl << endl;
return;
}
void CreateFile(){
fstream file;
char name[81];
cout << "Enter the file name : ";
cin >> name;
file.open(name,ios::out);
cout << "The file created successfully" << endl;
return;
}
void input(){
fstream file;
char ch;
char name[81];
cout << "Enter the file name : ";
cin >> name;
file.open(name,ios::app);
cout << "End your text , End by ( ~ )" <<endl;
cout << "Write in the file : ";
cin.get(ch);
while(ch!='~'){
file.put(ch);
cin.get(ch);
}
return;
}
int CountWords(string name){
fstream file;
string word;
int j=0;
file.open(name,ios::in);
while(!file.eof() && !file.fail() && file >> word){
j+=1;
}
return j;
}
int countChar(string name){
fstream file;
char data;
int i=0;
file.open(name,ios::in);
while(!file.eof() && !file.fail() && file >> data){
if(isalpha(data)){
i+=1;
}}
return i;
}
void FindWord(string name){
fstream file;
string word, Find;
int count=0;
cout<<"enter the word you want to search: ";
cin>>Find;
file.open(name,ios::in);
while(!file.eof()&& !file.fail()){
file >> word;
if(word==Find) count+=1;
}
if(count>=1) cout<<"the word is found"<<endl << endl;
else cout<<"the word is not found"<<endl << endl;
}
void Empty(string name){
fstream file;
file.open(name,ios::out);
cout<<"the file is empty successfully" << endl << endl;
}
void UpperCase(){
fstream file;
char name[81] , ch;
vector<char> container;
cout << "Enter the file name : ";
cin >> name;
file.open(name,ios::in);
while(!file.eof() && !file.fail()){
file.get(ch);
container.push_back(toupper(ch));
}
file.close();
file.open(name,ios::out);
for(int i=0 ; i<container.size()-1 ; ++i){
file.put(container[i]);
}
file.close();
return;
}
void LowerCase(){
fstream file;
char name[81] , ch;
vector<char> container;
cout << "Enter the file name : ";
cin >> name;
file.open(name,ios::in);
while(!file.eof() && !file.fail()){
file.get(ch);
container.push_back(tolower(ch));
}
file.close();
file.open(name,ios::out);
for(int i=0 ; i<container.size()-1 ; ++i){
file.put(container[i]);
}
file.close();
return;
}
void ReplaceWord(string name){
fstream file , temp;
char get[50];
string word , modify;
cout << "Enter existing word: ";
cin >> word;
cout << "Enter new word: ";
cin >> modify;
file.open(name,ios::in);
temp.open("temp.txt",ios::out);
while(!file.eof() && !file.fail()){
file.getline(get,50,' ');
if(get==word) temp << modify << ' ';
else temp << get << ' ';
}
file.close();
temp.close();
temp.open("temp.txt",ios::in);
file.open(name,ios::out);
while(!temp.eof() && !temp.fail()){
temp.getline(get,50,' ');
file << get << ' ';
}
return;
}
void AddFile(){
fstream main , add ;
char name1[81] , name2[81] , ch;
cout << "Enter The main File : ";
cin >> name1;
cout << "Enter The file You want to Add : ";
cin >>name2;
main.open(name1,ios::app);
add.open(name2,ios::in);
while(!add.eof() && !main.fail() && !add.fail()){
add.get(ch);
main.put(ch);
}
return;
}
void Save(string name){
fstream load , file;
char New[81] , ch;
cout << "Enter the file name you want to save in it : ";
cin >> New;
if(New != name){
load.open(name,ios::in);
file.open(New,ios::out);
while(!load.eof() && !load.fail()){
load.get(ch);
file.put(ch);
}}
cout << "file saved successfully " << endl;
return;
}
void Cipher(string name){
fstream file,temp ;
char ch[50] , cipher,word , rep ;
string sentence;
file.open(name,ios::in);
temp.open("temp.txt",ios::out);
cout << endl;
while(!file.eof()&&!file.fail() && file >> sentence){
for (int i=0; i<sentence.length() ;i++){
if((sentence[i] > 64) && (sentence[i] < 91) || ((sentence[i] > 96) && (sentence[i] < 123)))
cipher = (int)sentence[i] + 13;
else
cipher = (int)sentence[i];
if ((cipher < 0) || (cipher > 122))
cipher = cipher - 122 + 96;
cout << cipher;
temp << cipher;
}
cout << " ";
temp << " ";
}
file.close();
temp.close();
temp.open("temp.txt",ios::in);
file.open(name,ios::out);
while(!temp.eof() && !temp.fail()){
temp.getline(ch,50,' ');
file << ch << " ";
}
cout << endl << endl;
return;
}