Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > File Manipulation Code Examples

This Program Will Encrypt And Decrypt Any File Text Document

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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* This Program Will Encrypt And Decrypt Any File Text Document Provides The Maximum Security This Program Will Encrypt Any Type Of Text Document */ #include<fstream.h> #include<conio.h> #include<stdio.h> int main() { char name[30],target[30],ch,mod; //Declare Variables int num[100],i,option; cout<<"Enter Your Option "; cout<<" 1. To Encrypt The File "; cout<<" 2. To Decrypt The File "; cout<<" Option : "; cin>>option; if(option==1) { cout<<"Enter The Path Of A File Which Is To Be Encrypted : "; gets(name); ifstream fin(name,ios::binary); //Open The Input File In A Binary Mode if(!fin) { //Show Error Occur If File Does Not Exist cout<<" Error In Openinig Of A File : "; //Or Any Error Occurs return 1; } cout<<" Enter The New Encrypted File Name : "; gets(target); ofstream fout(target,ios::binary); //Open The OutPut File In A Binary Mode if(!fout) { cout<<" Error In Opening Of Target File : "; //Show Error If Any Error Occrs In Opening Of A File return 1; } for(i=0;i<9;i++) { //Multiple For Loops For Storing The Numbers num[i]=i; //In An Array } for(i=14;i<31;i++) //Loops Will Store 100 Numbers { num[i-5]=i; //Which Will Encrypt The Contents Of A File } for(i=33;i<=68;i++) //To Avoid The Error Ocuur Caused By The { //Enter Key , Tab Key & Space Key num[i-7]=i; //These Variations In Loops Is Made } for(i=97;i<=122;i++) { num[i-35]=i; } while(fin) { //Open The Input File fin.get(ch); if(ch==EOF)break; //Exit To Loop When End Of File if((ch>=97) && (ch<=122)) { //Encrypt The Small Letters i=97; mod=num[ch-i]; fout<<mod; } if((ch>=65) && (ch<=90)) { i=39; //Encrypt The Capital Letters mod=num[ch-i]; //And Store In An Output File fout<<mod; } if((ch>=48) && (ch<=57)) { i=4; //Encrypt The Numbers mod=num[ch+i]; fout<<mod; } if((ch==10)||(ch==13)) { mod=ch; //For Enter Key fout<<mod; } if(ch==32) fout<<ch; //For Space Key if(ch==9) fout<<ch; //For Tab Key if((ch>=33)&&(ch<=47)) { //For Special Symbols mod=ch+64; fout<<mod; } if((ch>=58)&&(ch<=64)) { //For Special Symbols mod=ch+54; fout<<mod; } if((ch>=91)&&(ch<=96)) { mod=ch+28; //For Special Symbols fout<<mod; } if((ch>=123)&&(ch<=126)) { mod=ch-40; //For Special Symbols fout<<mod; } } fin.close(); //Close The Input File fout.close(); //Close The Output File cout<<" Your File Is Encrypted Now........... "; getch(); return 0; } /*======================================================================== =======================================*/ /* This Program Will Decrypt The Document */ if(option==2) { char name[30],target[30],ch,mod; //Declare Variables int num[100],i,flag; cout<<"Enter The Path Of A File Name Which Is To Be Decrypted : "; gets(name); ifstream fin(name,ios::binary); if(!fin) //Open The Encryped File In A Binary Mode { cout<<"Error In Opening Of A File : "; return 1; //Show Error If File Does Not Exist } //Or Any Occurs In Opening Of A File cout<<" Enter The New Decrypted File Name : "; gets(target); ofstream fout; fout.open(target,ios::binary); //Opens The Output File In An Binary Mode if(!fout) { //Show Error If Any Error Occurs In Opening Of A File cout<<"Error In Opening Of A Target File : "; return 1; } for(i=0;i<9;i++) { //Same Multiple For Loops For Storing The Numbers num[i]=i; //In An Array } for(i=14;i<31;i++) { num[i-5]=i; //Loops Will Store 100 Numbers } for(i=33;i<=68;i++) //Which Encrypts The Document Also Decrypt It { num[i-7]=i; } while(fin) { //Opens The Encryped File fin.get(ch); flag=0; //Turn Off Flag if(ch==EOF)break; for(i=26;i<52;i++) { if(ch==num[i]) //Loop For Match The Small Letters Letters { mod=i+39; //If Match Then Put Appropriate Letter fout<<mod; //In A OutPut File flag=1; break ; //Turn On Flag And Exit The Loop } } if (flag==1) continue ; //If Flag Is On Then Continue Outer Loop for(i=0;i<26;i++) { //Loop For Match The Capital Letters if(ch==num[i]) { //If Match Then Put Appropriate Letter mod=i+97; //In A OutPut File fout<<mod; flag=1;break; //Turn On Flag And Exit From This Loop } } if (flag==1) continue ; //If Flag Is On Then Continue Outer Loop for(i=52;i<62;i++) { //Loop For Numerical Numbers if(ch==num[i]) { mod=i-4; fout<<mod; flag=1; break ; } } if (flag==1) continue ; if((ch==10)||(ch==13)) { mod=ch; //Condition For Enter Key fout<<mod; } if(ch==32) fout<<ch; //Condition For Space Key if(ch==9) fout<<ch; //Condition For Tab Key if((ch>=97)&&(ch<=111)) { mod=ch-64; //For Special Symbols fout<<mod; } if((ch>=112)&&(ch<=118)) { mod=ch-54; //For Special Symbols fout<<mod; } if((ch>=119)&&(ch<=124)) { mod=ch-28; //For Special Symbols fout<<mod; } if((ch>=83)&&(ch<=86)) { //For Special Symbols mod=ch+40; fout<<mod; } } fin.close(); //Close The Encrypted File fout.close(); //Close Your Original Decrypted File cout<<" The File Is Being Decrypted............ "; getch(); return 0; } return 0; }

Get characters. Extracts characters from the stream, as unformatted input. The get() function is used to read a character(at a time) from a file. The classes istream and ostream define two member functions get(), put() respectively to handle the single character input/output operations. There are two types of get() functions. Both get(char *) and get(void) prototype can be used to fetch a character including the blank space,tab and newline character. The get(char *) version assigns the input character to its argument and the get(void) version returns the input character. Since these functions are members of input/output Stream classes, these must be invoked using appropriate objects.

In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively. To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is: • ofstream: This data type represents the output file stream and is used to create files and to write information to files. • ifstream: This data type represents the input file stream and is used to read information from files. • fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

Continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration. The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

Open file. Opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails. The file association of a stream is kept by its internal stream buffer: Internally, the function calls rdbuf()->open(filename,mode|ios_base::out). The function clears the stream's state flags on success (setting them to goodbit). In case of failure, failbit is set.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Get string from stdin. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached. The newline character, if found, is not copied into str. A terminating null character is automatically appended after the characters copied to str. Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows). On success, the function returns str.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.


Swap numbers means exchange the values of two variables with each other. Variable num1 contains 20 and num2 contains 40 after swap there values num1 contains 40 num2 contains








C++ language allows us to separate program-specific datatypes through the use of classes. "C++ Classes" define types of data structures and the functions that operate on those data