Created
February 26, 2022 15:25
-
-
Save AnthoniG/37a0f3ce0e919b754f53b190ccd380ff to your computer and use it in GitHub Desktop.
C++ file handling for beginners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <fstream> | |
| using namespace std; | |
| int main() | |
| { | |
| fstream Myfile; | |
| // Taking the input string from the user | |
| string story; | |
| cout << "Enter a story: \n"; | |
| getline(cin, story); | |
| // Converting the string to character array | |
| char story_char[story.length()]; | |
| for (int i = 0; i < story.length(); i++) | |
| { | |
| story_char[i] = story[i]; | |
| } | |
| // Ciphering the story and writing it into the file..... | |
| Myfile.open("story.txt", ios::out); | |
| if (Myfile.is_open()) | |
| { | |
| for (int i = 0; i < story.length(); i++) | |
| { | |
| Myfile << int(story_char[i]); | |
| } | |
| } | |
| Myfile.close(); | |
| // De-ciphering the numbers into story..... | |
| cout << "The deciphered numbers form the story:\n "; | |
| Myfile.open("story.txt", ios::in); | |
| if (Myfile.is_open()) | |
| { | |
| string line; | |
| while(getline(Myfile, line)){ | |
| int num = 0; | |
| for(int i = 0; i < line.length(); i++){ | |
| num = num * 10 + (line[i] - '0'); | |
| if(num>=32 && num<=122){ | |
| char ch = (char)num; | |
| cout << ch; | |
| num = 0; | |
| } | |
| } | |
| } | |
| } | |
| Myfile.close(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment