This program will take the original file, output file, and an encryption key. This program will read the characters from the original file into a multidimensional array, transposing the array and writing the characters from the transposed array to the output file.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void ReadCharacters(std::ifstream &input_file,char **arr,int n);
// Post: The user has been prompted and has entered the elements of 'a'
// Pre: 'arr' is an 'n' by 'n' matrix.
void TransposeArray(char **arr,int n);
// Post: The elements of 'a' have been printed by row to standard output.
// Pre: 'a' is an 'n' by 'm' matrix.
void WriteCharacters(std::ofstream &output_file,char **arr, int n);
// Post: The elements of 'a' have been printed by row to standard output.
// Pre: 'arr' is an 'n' by 'n' matrix.
int main()
{
ifstream inFileName; //stream for input file
ofstream outFileName; //stream for output file
string file_name; //filename
char **arr; //array to store file
//reading file from user;
cout << "Enter the name of the original file --> ";
cin >> file_name;
cout << endl << endl;
inFileName.open(file_name.c_str());
// Check to make sure it opened
if( inFileName.fail() )
{
cout << "Error opening input file." << endl;
exit(EXIT_FAILURE);
}
// while(!inFileName.is_open());
cout << "Enter the name of the output file --> ";
cin >> file_name;
cout << endl << endl;
//taking encryption key from user
cout << "Enter the encryption key --> ";
int n;
cin >> n;
cout << endl << endl;
//opening output file name
outFileName.open(file_name.c_str());
if( outFileName.fail() )
{
cout << "Error opening output file." << endl;
exit(EXIT_FAILURE);
}
//creating 2-D array
arr = new char*[n];
for(int i = 0; i < n; i++)
arr[i] = new char[n];
// Read each character in the file.
while(!inFileName.eof()){
//function to read file from user
ReadCharacters(inFileName,arr,n);
//transpose an array
TransposeArray(arr,n);
//writing array to file
WriteCharacters(outFileName,arr,n);
}
//closing both input and output file
inFileName.close();
outFileName.close();
return 0;
}
void ReadCharacters(std::ifstream& input_file,char **arr,int n)
{
//read file char by char
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
input_file.get(arr[i][j]);
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
void TransposeArray(char **arr,int n)
{
//transpose 2-D array
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
char temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
void WriteCharacters(std::ofstream &output_file,char **arr, int n)
{
//writing array to file
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
output_file.put(arr[i][j]);
}
}
}
TEXT FILE: Victor. 13015585. The quick brown fox jumps on the lazy dog.
2-Dimensional Array:
V i c t o
r . 1 3
0 1 5 5 8
5 .
T h
e q u i
c k b r
o w n f
o x j u
m p s o
n t h e
l a z y
d o g .
n s t
b j h
r f u o e
Aucun commentaire:
Enregistrer un commentaire