w3resource

C++ File handling: Decrypting the contents of a text file

C++ File handling: Exercise-13 with Solution

Write a C++ program to decrypt the contents of a text file encrypted using the above algorithm.

Sample Solution:

C Code:

#include <iostream>   // Including the input/output stream library
#include <fstream>    // Including the file stream library
#include <string>     // Including the string handling library

// Function to display the content of a file
void displayFileContent(const std::string & filename) {
  std::ifstream file(filename); // Open file with given filename for reading
  std::string line; // Declare a string to store each line of text

  if (file.is_open()) { // Check if the file was successfully opened
    std::cout << "File content:" << std::endl; // Displaying a message indicating file content
    while (std::getline(file, line)) { // Read each line from the file
      std::cout << line << std::endl; // Display each line of the file
    }
    file.close(); // Close the file
  } else {
    std::cout << "Failed to open the file." << std::endl; // Display an error message if file opening failed
  }
}

// Function to decrypt a file using a simple algorithm (decrementing ASCII values)
void decryptFile(const std::string & inputFile, const std::string & outputFile) {
  std::ifstream input(inputFile); // Open input file for reading
  std::ofstream output(outputFile); // Open or create output file for writing

  if (input.is_open() && output.is_open()) { // Check if both files were successfully opened
    char ch; // Declare a character variable to read characters from the input file

    while (input.get(ch)) { // Loop through each character in the input file
      ch--; // Simple decryption algorithm: Decrement ASCII value by 1
      output.put(ch); // Write the decrypted character to the output file
    }

    input.close(); // Close the input file
    output.close(); // Close the output file

    std::cout << "File decrypted successfully.\n" << std::endl; // Display a success message
  } else {
    std::cout << "Failed to open the files.\n" << std::endl; // Display an error message if file opening failed
  }
}

int main() {
  std::string inputFile = "encrypted_test.txt"; // Input file (encrypted)
  displayFileContent("encrypted_test.txt"); // Display content of "encrypted_test.txt"
  std::cout << std::endl; // Output a newline for formatting

  std::string outputFile = "decrypted_test.txt"; // Output file (decrypted)
  decryptFile(inputFile, outputFile); // Decrypt "encrypted_test.txt" and write to "decrypted_test.txt"
  displayFileContent("decrypted_test.txt"); // Display content of "decrypted_test.txt"
  std::cout << std::endl; // Output a newline for formatting

  return 0; // Return 0 to indicate successful execution
}

Sample Output:

File content:
D,,!jt!b!ijhi.mfwfm-!hfofsbm.qvsqptf!qsphsbnnjoh!mbohvbhf!dsfbufe!cz!Ebojti!dpnqvufs!tdjfoujtu!Ckbsof!Tuspvtusvq/!♂Gjstu!sfmfbtfe!jo!2:96!bt!bo!fyufotjpo!pg!uif!D!qsphsbnnjoh!mbohvbhf-!ju!ibt!tjodf!fyqboefe!tjhojgjdboumz!pwfs!ujnf/!♂Npefso!D,,!dvssfoumz!ibt!pckfdu.psjfoufe-!hfofsjd-!boe!gvodujpobm!gfbuvsft-!jo!beejujpo!up!gbdjmjujft!gps!mpx.mfwfm!nfnpsz!nbojqvmbujpo/♂Ju!jt!bmnptu!bmxbzt!jnqmfnfoufe!jo!b!dpnqjmfe!mbohvbhf/♂Nboz!wfoepst!qspwjef!D,,!dpnqjmfst-!jodmvejoh!uif!Gsff!Tpguxbsf!Gpvoebujpo-!MMWN-!Njdsptpgu-!Joufm-!Fncbsdbefsp-!Psbdmf-!boe!JCN/

File decrypted successfully.

File content:
C++ is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup.
First released in 1985 as an extension of the C programming language, it has since expanded significantly over time.
Modern C++ currently has object-oriented, generic, and functional features, in addition to facilities for low-level memory manipulation.
It is almost always implemented in a compiled language.
Many vendors provide C++ compilers, including the Free Software Foundation, LLVM, Microsoft, Intel, Embarcadero, Oracle, and IBM.

Explanation:

In the above exercise,

  • The function decryptFile() takes two parameters: inputFile (the name of the input file to decrypt) and outputFile (the name of the output file to store the decrypted content).
  • The program opens the input file using std::ifstream and the output file using std::ofstream.
  • Next, it reads each character from the input file using std::ifstream::get and applies a simple decryption algorithm by decrementing the ASCII value of each character by 1.
  • The decrypted character is written to the output file using std::ofstream::put.
  • Both the input and output files are closed after the entire file has been decrypted.

Note: Content of a file is displayed using displayFileContent("filename.txt").

Flowchart:

Flowchart: Decrypting the contents of a text file.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Encrypt text file using a simple algorithm.
Next C++ Exercise: Reading and displaying CSV file in tabular form.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/file-handling/cpp-file-handling-exercise-13.php