#include <fstream>
#include <iostream>
usingnamespace std;
int main()
{
//Variables
fstream InFile, OutFile;
constint intSIZE = 200;
char charInputName[intSIZE];
char charOutputName[intSIZE];
//Title
cout << "\t\t*****Chapter 12.9: File Encryption Filter*****\n\n";
//Open the file
InFile.open("Secret.txt", ios::in);
//Validate text
if (!InFile)
{
cout << "ERROR: Unable to open file./n";
}
//Read the text file
cout << "Original File:\n";
for(InFile.getline(charInputName, intSIZE, '\n'); !InFile.eof();
InFile.getline(charInputName, intSIZE, '\n'))
{
for (int intCount = 0; intCount < strlen(charInputName); intCount++)
{
cout << charInputName[intCount];
charOutputName[intCount] = (charInputName[intCount] + 10);
}
}
//Encrpyt the file
cout << "\n\nEncrpyting file......\n";
for (int intCount = 0; intCount < strlen(charOutputName); intCount++)
{
cout << charOutputName[intCount];
}
InFile.close();
system ("PAUSE");
return 0;
}
The project is a file encrpytion program. The program is to read a file (which is two sentences) and to add 10 to the ASCII code to "decrpy" it. The problem is that the file reads extra "characters".
Example:
The program will read a text file with this: "This is a secret message. The bear is not blue".
Which is around 50 characters, but the outputfile contains somewhere around 288 characters have "10" is added to the ASCII code of the 50 characters. I can't figure out where these extra characters are coming from. Thanks in advance!
I think the extra characters are there because you aren't initializing your array, so there is no terminating null character on the end for the string.
Use std::string/getline(InFile, some_std_string) instead to avoid these kinds of problems...
Because there isn't '\0' in charOutputName , the result of strlen(charOutputName) is unsure. So it will output the ASCII code until the first '\0' in memory.
initialize the char array before using it.
or add '\0' in the end of the string
And your code has another problem, it will store last line because every loop you will rewrite the 'charOutputName'.(Unless you write file in the loop)