Member Avatar for pinkboom

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).

Here is the code only for the file read and encryption.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;

ofstream f3("crypted.txt");
char text3[256];
unsigned int i=0;
int j,b=0;
void line_encrypt();
void file_read();
int main()
{
	file_read();
	cin.get();
	return 0;
}
void file_read()
{
	ifstream f2("source.txt");
	if (f2.is_open())
	{
		while(f2.good())
		{
			//f2>>text3; Reading Word with word, no way to save the newlines
			f2.getline(text3,256);	
			j=strlen(text3);cout<<" "<<j;text3[j]=' ';/*if I set the character replaced with '\n', it screws up the encryption algorithm.*/
			line_encrypt();
		}
		f2.close();f3.close();
	}
	else cout<<"error...";
	memset(text3, '\0', sizeof(text3));
}

void line_encrypt()
{
	i=0;
	for (i=0;i<=j;i++) {
		if(b==0) {
			text3[i]=text3[i]+'3'-'1'; b=1; }
		else {
			text3[i]=text3[i]-'5'+'2';b=0; }
		f3<<text3[i];
	}
}

The way the code is right now, it DOES not save any newlines..

So guys, I'm asking for your help, because I've wasted about 4 hours on this problem, trying various solutions and it DID not work.

Thanks.

pinkboom

LE:

this is the decryption algorithm:

void decrypt() {	
	ifstream f2("source.txt");
	ofstream f3("decrypted.txt");
	unsigned int i=0;
	int b=0;
	 if (f2.is_open())
	  {
		while ( f2.good() )
		{
			f2>>text2[i];i++;
		}
		f2.close();
	  }
	 cout<<endl;
	 for (i=0;i<strlen(text2);i++) {
       if(b==0) {text2[i]=text2[i]+'1'-'3'; b=1;}  //strcpy(text,text+'5'); b=1;}
       else { text2[i]=text2[i]-'2'+'5';b=0;}
	   f3<<text2[i];
	   cout<<text2[i];
       }
	memset(text2, '\0', sizeof(text2));
	f3.close();
	cin.get();
}

You need to insert f3 << std::endl; after line 46 and before line 47.

Member Avatar for pinkboom

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.

//source.txt
I am Legend
Are you legend?
Really?

asdfasdf

pinkboom	
boom


Your argument is invalid.
//crypted.txt
Kcj"Igdgkf
Cog{lwnbibpaA
TbcinvA
"
^uah^uah
"
mkkm_qlo"
_qlo
"

[lwo"^tdwjgkvkp"fpscika0
//decrypted.txt
I am Legend Are you legend? Really?  asdfasdf  pinkboom	 boom   Your argument is invalid.

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)

Member Avatar for pinkboom

Well I was planning on using the string function, but I can't figure out how to encrypt it the same way I did it with the character array.

Member Avatar for pinkboom

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.

void file_read()
{	unsigned int i=0;int b=0;
	string text3; char ch;
	ifstream f2("source.txt");
	ofstream f3("crypted.txt");
	if (f2.is_open())
	{
		while(f2.good())
		{
			while( f2.get(ch) ) {
				text3[i]=ch;
				if(b==0) {
					text3[i]=text3[i]+'3'-'1'; b=1; }
				else {
					text3[i]=text3[i]-'5'+'2';b=0; }
				f3<<text3[i];
				i++;
			}
		}
		f2.close();f3.close();
	}
	else cout<<"error...";
}

Source.txt is the same as before.

//source.txt
I am Legend
Are you legend?
Really?

asdfasdf

pinkboom	
boom


Your argument is invalid.

After encrypting:

//crypted.txt
Kcj"IgdgkfCog{lwnbibpaATbcinvA^uah^uahmkkm_qlo_qlo[lwo"^tdwjgkvkp"fpscika0

After decrypting:

//decrypted.txt
I am Legend
Are you legend?
Really?
\x_k\x_kpinkboom	]tjr
Your argument is invalid.

I have no clue why that happens.

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.

Member Avatar for pinkboom

Yes!! It finally works. It was a flaw in the decrypt function.
Thanks for your patience ravenous! :)

Here's the encrypt and decrypt function:

//file read+encrypt
void file_read()
{	int i=0;int b=0;
	char text3[512];
	ifstream f2("input.txt");
	ofstream f3("crypted.txt");
	if (f2.is_open())
	{
		while(f2.good())
		{
            char ch;
            f2.get(ch);
			text3[i]=ch;
			if(b==0) {
				text3[i]=text3[i]+'3'-'1'; b=1; }
			else {
				text3[i]=text3[i]-'5'+'2';b=0; }
			f3<<text3[i];
			i++;
		}
		f2.close();f3.close();
    }
    else cout<<"error...";

}

And the decrypt function:

//decrypt
void decrypt_file() {
	ifstream f2("source.txt");
	ofstream f3("decrypted.txt");
	unsigned int i=0;
	int b=0;
	 if (f2.is_open())
	  {
        while ( f2.good() )
        {
            f2.get(text2[i]); i++;
        }
		f2.close();
	  }
	 cout<<endl;
	 for (i=0;i<strlen(text2);i++) {
       if(b==0) {text2[i]=text2[i]+'1'-'3'; b=1;}  //strcpy(text,text+'5'); b=1;}
       else { text2[i]=text2[i]-'2'+'5';b=0;}
	   f3<<text2[i];
	   cout<<text2[i];
       }
	memset(text2, '\0', sizeof(text2));
	f3.close();
	cin.get();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.