How do i get my function to read the entire file and what is wrong with my function

I am trying to read in all the letters and numbers from a file and change them with a caesar cypher code. (For example, if k = 3, then the letter A would be replaced with the letter D). I have a text file that is as follows

==========
Here are SOME letters.
Letters and 1234 on this line
===========

I am trying to change them. I have this code (Below) and when I set keyNumber to 2 (Meaning if the letter was an A, than it would change into a C), I just get this.

===========
[> > (basically giberish)
===========

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

cout << "Number to move here"<<endl;
		cin >> keyNumber;

void encryptfile(ifstream &inputfile, int keyNumber, ofstream &outputfile)
{
	char next, cipher;
	
	while(!(inputfile.eof()))
	{
		inputfile.get(next);
		
		if(isalpha(next))
		{
			if(isupper(next))
			{
				cipher = (next + keyNumber)% 26;
				outputfile << cipher;
			}
			else //if lowercase
			{
				cipher = (next + keyNumber)% 26;
				outputfile << cipher;			
                        }
		}

		encryptfile(inputfile, keyNumber, outputfile);	
	}	


NOW, another problem is that lets say I don't have the cipher function there. I just say

1
2
3
4
is(upper(next))
{
    outputFile << "a";
}


then, it would still only read the first word before the Space. How do I fix that?
Last edited on
If you use operator >> or operator << it will skip whitespace, which is why doing cin >> someStr will only get you
this
if you enter
this is a line
in the command line. That can present a whole host of other issues if you aren't aware of that, but I won't get into it here.

You can do what you're doing now by using get to retrieve a character. You can use put to place a single character into your output file (see http://www.cplusplus.com/reference/ostream/ostream/put/).

You'll also want to check your value-wrapping logic and make sure you clamp your char values properly. For uppercase, you'll want to clamp it between 65 and 90. For lowercase, you'll want to clamp it between 97 and 122 (see http://en.cppreference.com/w/cpp/language/ascii), unless you want letters to be able to turn into non-alphabetic characters (eg 'A' turns into '*' with the right value), but that would be a somewhat odd shift-cipher.

Also, you can use isspace to check if a characer is a whitespace character so you can ignore it (see http://www.cplusplus.com/reference/cctype/isspace/), unless you want to handle alpha and numerics differently, in which case you're probably fine.

TLDR: use operator << and operator >> to skip over whitespace. Use get and put to read character-by-character.
You could also read the entire file into a buffer and then iterate over that buffer, which would be arguably faster.

Also, is that function supposed to be recursive? I don't think you'll want that to be recursive.
even when I change the function to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

while(input.get(characters))
{

if(isalpha(characters)
{
       if(isupper(characters)
         {
                 output.get(characters)
         }

       if(islower(characters)
         {
                 output.get(characters)
         }
}
}


When trying to read the file

1
2
Here are SOME letters
Here are some 1234


all I get as the result is

 
abbb


Is there anyway to go through the entire two lines?
Last edited on
Topic archived. No new replies allowed.