I'm having trouble trying to read a text file called "decrypted.txt" into my code while using the following code. I'm trying to input the text file into an array of chars so that once I get into the fixCapitalization function, I can iterate so that any letter after "." or "space" will capitalize the letter after it. I'm not sure if string will works in the iteration process. Any help would be great thanks!
1 2 3 4 5
for (int i = 0; i < message[length]; i++)
{
getline(inputFile, message[length]);
}
#ifndef MESSAGE_H_
#define MESSAGE_H_
#include <iostream>
#include "Message.h"
#include <string>
#include <fstream>
#include <string>
usingnamespace std;
class Message
{
private:
char *message; // holds the message
int length; // holds the the message length
staticconstshort ALPHABET_SIZE = 26;
char code[ALPHABET_SIZE]; // holds the cypher alphabet
// iztohndbeqrkglmacsvwfuypjx
// ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth
// returns the input file size in bytes
streamsize getFileSize(fstream &file) const
{
streamsize fsize = 0;
file.seekg(0,ios::end);
fsize = file.tellg();
file.seekg(0,ios::beg); // moves file pointer back to the beginning
return fsize;
}
public:
/*
* This constructor tries to open the file whose name is passed
* to it in filename. If file opens successfully, calls function
* getFileSize to determine how many bytes should be allocated
* for the message. Allocates space for message and reads the
* content from the file into it. Closes the file at the end.
* Member variable length should be set to the file size.
* If file cannot be found, length should be set to zero.
*/
Message(string messageFile)
{
//Open the message file
fstream inputFile;
inputFile.open("Encrypted.txt");
//Check if the input file has successfully open
while (inputFile.fail())
{
cout << "Input file has failed to open." << endl;
}
//Call function getFileSize and read into an array of message
length = getFileSize(inputFile);
char *message = newchar;
*message = message[length];
//Read the file
for (int i = 0; i < message[length]; i++)
{
getline(inputFile, message[length]);
}
//Check to see if the file was read successfully
for (int i = 0; i < message[length]; i++)
{
cout << message[i] << endl;
}
}
/* The destructor frees the space allocated to message
virtual ~Message();
// Decodes the message
void decode();
// Capitalizes first letter in each sentence
void fixCapitalization();
// Prints the content of message on the screen
void dump() const;
// Returns true if the message is empty
bool isEmpty() const;
*/
};
#endif /* MESSAGE_H_ */
int main()
{
/*//Create a message object with the content of Encrypted.txt
Message m("Encrypted.txt");
//Check if the fie was succesfully opened
if (m.isEmpty())
{
cout << "Could not read message. Make sure the file has been implemented correctly in the code or the location "
<< "is in the same folder." << endl;
return EXIT_FAILURE;
}
//Cout the original message
cout << "Original message: " << std::endl;
m.dump(); //Function that couts the original message
cout << endl << endl;
//Call the function that decodes the message
m.decode();
//Call the function that fixes the capitalization of the function
m.fixCapitalization();
//Call the function that prints the doceded message
std::cout << "Decoded message: " << std::endl;
m.dump();
cout << endl << endl;
return EXIT_SUCCESS;
*/
system("pause");
}
This is the contents of the decrypted.txt (not sure f it will help)
ifqkwxcadf ar cei fpoi masif cd cei xkdqirr du pxxnwafm pf pnmdkaceo cd p oirrpmi, teaqe rqkpohnir cei gpcp af ac-oplafm ac sikw gauuaqvnc pfg caoi qdfrvoafm, au fdc xkpqcaqpnnw aoxdrrahni, cd gigvqi cei dkamafpn masif dfnw cei ifqdgig gpcp. afxvcr cd cei pnmdkaceo cwxaqpnnw afsdnsi pggacadfpn riqkic gpcp qpnnig liwr, teaqe xkisifcr cei oirrpmi ukdo hiafm giqdgig-isif au cei pnmdkaceo ar xvhnaqnw lfdtf.