Problems with File Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <fstream>
#include <iostream>
using namespace std;


int main()
{
	//Variables
	fstream InFile, OutFile;
	const int 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.
1
2
3
4
5
6
7
	//Variables
	fstream InFile, OutFile;
	const int intSIZE = 200; 
	char charInputName[intSIZE];
	char charOutputName[intSIZE];
	memset(charInputName, 0, intSIZE);
	memset(charOutputName, 0, intSIZE);

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
                //Read the text file
                int intOutCount = 0;
	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];//write file ?
				 charOutputName[intOutCount ] = (charInputName[intCount] + 10);
				 intOutCount++;
			 }
		}		 
                charOutputName[intOutcount] = '\0';

That did it, thanks!
Topic archived. No new replies allowed.