Not reading cypertext

I have been messing around with this for a little while now. I have been trying to make a system to load a file into ram and then into a string. From there it will be destroyed and the ram will be cleared. I've made this for another encryption system that I have been working on.
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cstddef>
#include <vector>
#include <map>
#include <array>
#include <stdio.h>

using namespace std;	


int main ()
{
	string *fileName = new string;
	string *txt = new string;
	getline(cin,*fileName);
	ifstream file(*fileName,ios::in|ios::binary|ios::ate);
	if (file.is_open())
    while (file.good())
    {
	getline(file, *txt);
    }
	file.close();
	//cout << *txt << endl;
	
	system("pause");
	*txt = "NULL";
	delete(txt);
	txt = new string;
	cout << "free" << endl;
	system("pause");
}
1) WHat is exactly your problem?
2) Why do you use excessive unneeded pointers? Especially when you managed to have memory leak there.

Edit: You do understand that read position in your file points at the end of your file, so any read will not read anything, do you?
Last edited on
Simplified:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main ()
{
	string fileName,txt;
	cin>>fileName;
	ifstream file;
	file.open(fileName);
    while(file.good())
    {file>>txt;}
	file.close();
	system("pause");
	cout << "free" << endl;
	system("pause");
}
Thanks jasonwynn10
And MiiNiPaa then how would I defeat the memory leak.
if you follow jasonwynn's example, there is no memory leak. Hoever if you use your example it would simple be delete fileName.
Since the memory leak was caused when you did not deallocate the memory associated with the string pointer.
The code has massive problems reading anything other than stories. It wont read cypher text or anything other than basic English.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main ()
{
	string fileName,txt;
	
	getline(cin,fileName);
	
	ifstream file;
	
	file.open(fileName);
	//,ifstream::binary
    while(file.good())
    {file>>txt;}
	
	file.close();
	cout << txt << endl;
	system("pause");
	cout << "free" << endl;
	system("pause");
}


If there is any confusion I stole this code from a multiplayer mod.
1
2
3
4
    fileIn.open(*fileName, ios::in | ios::binary);
    string str((istreambuf_iterator<char>(fileIn)), istreambuf_iterator<char>()); 
	*data = str;
    fileIn.close();

I used the code above to run the code below if anyone can help than that would be create if possible.
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
void decodeXtea(unsigned int* v, unsigned int* w, unsigned int* k) {
    register unsigned int v0=v[0], v1=v[1], i, sum=0xC6EF3720;
    register unsigned int delta=0x9E3779B9;
    for(i=0; i<32; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
    }
    w[0]=v0; w[1]=v1;
}

void TeaDecode ( const std::string& str, const std::string& key, std::string* out )
{
    unsigned int v[2];
    unsigned int w[2];
    unsigned int k[4];
    unsigned int keybuffer [ 4 ];

    // Clear buffers
    memset ( v, 0, sizeof(v) );
    memset ( w, 0, sizeof(w) );
    memset ( k, 0, sizeof(k) );
    memset ( keybuffer, 0, sizeof(keybuffer) );
    out->clear ();

    // Count the number of passes that we need
    int numBlocks = str.length() / 4;
    int numPasses = numBlocks - 1;

    if ( numPasses <= 0 )
        return;

    // Process the key
    int len = key.length ();
    if ( len > 16 )
        len = 16;
    memcpy ( keybuffer, key.c_str(), len );
    for ( int i = 0; i < 4; ++i )
        k[i] = keybuffer[i];

    // Create a temporary buffer to store the result
    unsigned char* buffer = new unsigned char [ numPasses * 4 + 4 ];
    memset ( buffer, 0, numPasses * 4 + 4 );

    // Decode it!
    const char* p = str.c_str();
    v[1] = *(unsigned int*)&p[numPasses * 4];
    for ( int i = 0; i < numPasses; ++i )
    {
        v[0] = *(unsigned int*)&p[(numPasses-i-1)*4];
        decodeXtea ( &v[0], &w[0], &k[0] );
        *(unsigned int*)&buffer[(numPasses-i-1)*4] = w[0];
        v[1] = w[1];
    }

    out->assign ( (char *)buffer, numPasses*4 );
    delete [] buffer;
}


The issue with the code that I am using is that I cannot free up ram and remove the data. Until the program closes and this creates problems with large files.
Last edited on
Topic archived. No new replies allowed.