Reading from file.

Hey guys,
I wrote a 'bigger' program that uses a basic algorithm to crypts a text I input from keyboard, or from another text file.
I have divided the program in the 'working' part and the 'non-working' part. The keyboard input is great, it works perfectly. But when I try reading from a file I have the following issues.

1. It doesn't save newlines.
2. If it does save newlines (code modifying) if there are more than 2 newlines, the coding algorithm messes up (it doubles the amount changed).

I have already tried that once, the problem is the following:
If I do that, in my output file, the newlines won't be "crypted" and when the decrypt program reads the file, it'll "decrypt" the newlines also, and the newlines won't be saved.

Ah, I see. In that case I don't think that you can read in the file line-by-line, since one of your encrypted characters might end up being the new line character itself. So, to do this properly, you should read and encrypt the file one character at a time (or at least read the whole file into a buffer and then encrypt the characters). The problem with using getline is that it discards the newline character, so you will always have a problem unless you add them back in again.

You should also use std::string unless you have a really good reason. It will let you get rid of some of your nasty global variables :O)

Okay here's what I found out. I did the reading character by character (hopefully), but after I encrypt the file, the decryption is a bit weird.
My reading function also encodes the character now. And I got rid of all the global variables.

I think your decrypt function is wrong, the >> operator is probably stripping the newline characters. Try using f2.get(text2[i]) instead of f2 >> text2[i] in the decrypt function.

In fact, I'd probably replace the whole of this bit:

while ( f2.good() )
{
f2 >> text2[i];
i++;
}

with this:

while ( f2.good() )
{
char ch;
f2.get(ch);
test2 += ch;
}

For strings the operator += is overloaded so that it appends things to the end of the string. The way you have it is likely to cause a segfault eventually, if you don't initialise the string to the right size at the start. This way, the string just keeps resizing as long as you keep adding characters.