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();
}