Something's Gone Wrong With Pointers!

I have an encrypter that uses a pointer to read the contents and then edit the value and output and en/decrypted version back to the text file.

here's my code

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

char* text;
int length = 0;

void login();
void read();
void encrypt();
void decrypt();

int main(){
    int a = 0;
    login();
    cout << "\nBefore using this program please ensure your data is placed in the text ";
    cout << "\ndocument named \"target.txt\" and placed in same location as the application!";
    do{
        cout << "\n\n0: Quit!\n1: Encrypt?\n2: Decrypt?\n\nPlease Select An Option: ";
        cin >> a;
        switch (a){
            case 0 : return 0; break;
            case 1 : system("CLS"); encrypt(); break;
            case 2 : system("CLS"); decrypt(); break;
            default : cout << "Invalid Selection!";
        }
    }while(a != 0);
    return 0;
}

void login(){
    string user = "SatsumaBenji";
    string pass = "Password";
    string userTry;
    string passTry;
    cout << "\n\n\tYou Must Login Before Using This Application!\n";
    do{
        cout << "\nUsername: ";
        cin >> userTry;
        cout << "Password: ";
        cin >> passTry;
        system("CLS");
        cout << "\n\n\tIncorrect Username or Password!\n";
    }while((userTry != user) || (passTry != pass));
    system("CLS");
    return;
}

void read(){
    
    ifstream ifile;
    ifile.open("target.txt", ios::binary);
    if(ifile.is_open()){
        ifile.seekg(0, ios::end); //Get length of file
        length = ifile.tellg();
        ifile.seekg(0, ios::beg); //Reset Position of pointer to 0
        text = new char[length]; //Set length of char* to file length
        ifile.read(text, length); //Read File upto Length limit
        ifile.close();
    }
    else
    cout << "\nError Opening File To Read!\n";
    return;
}

void encrypt(){
    int temp;
    char out;

    read();
    ofstream ofile;
    ofile.open("target.txt", ios::out | ios::trunc);
    if(ofile.is_open()){
        for(int i=0; i<length; ++i){
            temp = text[i];
            temp = (static_cast<int>(temp));
            temp = temp+((i+1)*3);
            out = (static_cast<char>(temp));
            ofile << out;
        }//For
        ofile.close();
        cout << "\nFile Encryption Complete!" << endl;
    }//If file open
    else
    cout << "\nError Opening File To Write!\n";
    return;
}//encrypt()


void decrypt(){
    int temp;
    char out;

    read();
    ofstream ofile;
    ofile.open ("target.txt", ios::out|ios::trunc);
    if(ofile.is_open()){
        for(int i=0; i<length; ++i){
            temp = text[i];
            temp = (static_cast<int>(temp));
            temp = temp-((i+1)*3);
            out = (static_cast<char>(temp));
            ofile << out;
        }//For
        ofile.close();
        cout << "\nFile Encryption Complete!" << endl;
    }//If file open
    else
    cout << "\nError Opening File To Write!\n";
    return;
}//decrypt() 


It works perfectly fine until it goes to multiple lines, Here's a test i did...

BEFORE ENCRYPTION

Hello There!
Hello There!
Hello There!
Hello There!
Hello There!


Hello There!
Hello There!
Hello There!
Hello There!
Hello There!


Hello There!
Hello There!
Hello There!
Hello There!
Hello There!


Hello There!
Hello There!
Hello There!
Hello There!
Hello There!

What's Up People


AFTER ECNRYPTION

E_c``?PJTDýæãàÝÚÔ)-**Ø	
Ç°­ª§¤žÙó÷ôô¢ÓäÞèØ‘zwtqnh£½Á¾¾l®¨²¢[DA>;82m‡‹ˆˆ6gxr|l%üüùöóðêêçäáÞØ-1..Ü
<"Ë´±®«¨¢Ý÷ûøø¦×èâìÜ•~{xurl§ÁÅÂÂp¡²¬¶¦_HEB?<6q‹ŒŒ:k|vƒzj#	 ý÷ñ,FJGGõ&71;+äÐÍÊÄÄÁ¾¸²µ²¯©©¦£—š—”ŽŽ‹ˆ‚|·ÑÕÒÒ€±Â¼Æ¶o[XUOOLIC=x’–““Arƒ}‡w0>[

þ9SWTT3D>H8ñÝÚ×ÑÑÎËÅ¿ú<&&Ãôÿ	ù²ž›˜’’Œ†€»ÕÙÖÖ„µÆÀʺs_\YSSPMGADA>8852,&)&#
]F`daa@QKUEþêçäÞÞÛØÒÌ!%""Ð[¿«¨¥ŸŸœ™“ÈåÜàÝÝ‹¼ÍÇÑÁzif`Z]ZTNHƒ¡žžL}Žˆ’‚;*'!&	D^b__
>OISCüëèâÜßÜÖÐÊÐÍÇÁÄÁ»µ¯ùý
½°âú§ÔæíëäÚ


AFTER DECRYPTION (back to before)

Hello There!


Hello There!


Hello There!


Hello There!


Hello There!








Hello There!


Hello There!


Hello There!


Hello Theuob

EbiilQebob





EbiilQebob

EbiilQebob

EbiilQebob

EbiilQebob

EbiilQebob





EbiilQebob

EbiilQebob

EbiilQebob

EbiilQebob

EbiilQebob



Te^q$pRmMblmib


As you can see it works fine so far and then changes and the rest of the data is lost.

(apologies i did the test multiple times for it to do this so i'm not sure if the error occurs during encryption or decryption).




Does anybody have any ideas why it's doing this or how i can fix it but keep it using the same methods it currently uses (pointer to read, casting for encryption, etc..). If no-one is able to help i know i should be able to set up a getline() loop but first i want to learn the errors i've made here!
Thanks.
Open the file for writing in binary mode, the same as you do for reading it in. Btw, you allocate memory via new every time read() is called, but never deallocate it.
Last edited on
thanks i'll try opeining in binary to see if that helps, also how doo i deallocate the memory once i've finished with it?

EDIT: Thank you greatly, it was such a simple mistake but it's working perfectly now!

I still need to know how to deallocate the memory though as this would be a largely improvement on efficiency.
Last edited on
I think it should use ios::out with the ios::binary and ios::trunc.
you can try it to use two ways ,
yeah i found out it works perfectly fine without ios::out
How do i deallocate memory when i no longer need it (hopefully there's just a simple function i can put at the end of my encryption functions)?
Last edited on
You could do it at the end of the encrypt and decrypt functions, yes.

delete [] text ;
Topic archived. No new replies allowed.