bbbiiiiggggg pointers problem..

Hi.. I'm Carmen and I'm new on this forum...
I am dealing with a big problem... I can't figure out why it gives me these errors... I am trying to filter a text, transform the lowercase in uppercase and leave all the other characters out. Then, I am picking some random letters and replace those from the filtered text with its equivalent. After this, I am trying to manipulate the changed text in order to find the real one. But I can't get to that point cause smth messes the things out and it gives segmentation fault or it doesn't give me any errors, but it doesn't reach the end of the function.
Can you please look on my code and give me some suggestions... I feel I can't get trough and every other homework depends on this one...
Please help
[code]

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <string.h>
#include <time.h>

using namespace std;

#define SIZE 100000
#define SIZE2 200
#define NR_L 2
#define NR_C 26



void loadFile(fstream& stream, string str){
const char* file;

file = new (nothrow) char [20];

if (file==0) cout << "Error: null pointer!\n";

file = str.c_str();

stream.open(file, ios::in|ios::out);

if (! stream.is_open())
{

cout << "loadFile: Errorin opening the file\n";

exit(1);

}
};

//how much letters are in the file
int checkLength(string str){

char c;

int length;

int i = 0, j = 0;

fstream stream;

loadFile(stream, str);

stream.seekg(0, ios::end);

length = stream.tellg();

stream.seekg(0, ios::beg);

while(!stream.eof()){

c = stream.get();

if(c == ' '){

i++;

}
if(c == '\n'){

j=j+2;

}
}


stream.close();
return length-i-j;
};

//filtering the text
const char* filterText(fstream& stream, string str){

int filtered_len;

string text;

string::iterator it;

int i = 0;

int text_len;

char buffer[SIZE];

const char* filtered_text;

filtered_len = checkLength(str);

filtered_text = new (nothrow) char [filtered_len+1];

if (filtered_text==0) cout << "Failed!\n";

loadFile(stream, str);

while(stream){

stream.getline(buffer, 100000);

text.append(buffer);

for(it = text.begin(); it != text.end(); ++it){

if((isalpha(*it) == 0)){

text.erase(it);

--it;

}

if(islower(*it)){

text.replace(it, it+1, 1, toupper(*it));

}
}

}
stream.close();
text_len = text.length();

text[text_len] = '\0';

filtered_text = text.c_str();

return filtered_text;
}

//substitution
const char* permutation(fstream& stream, string str){

string new_text, criptotext;

string alphabet;//the en alphabet

string random_alphabet;//the random generated alphabet

char letter;

int filtered_len, f_len, cripto_len;

int i, j;

const char* criptotext_ret;

filtered_len = checkLength(str);

criptotext_ret = new (nothrow) char [filtered_len + 1];

if (criptotext_ret==0) cout << "Error: null pointer!\n";

const char* filtered_text;

filtered_text = new (nothrow) char [filtered_len+1];

if (filtered_text==0) cout << "Error: null pointer!\n";

filtered_text = filterText(stream, str);
f_len = strlen(filtered_text);//the filtered text length

new_text.append(filtered_text, 0, f_len);

srand ( unsigned ( time (NULL) ) );

for(i = 65; i<=90; i++){
letter = (char)i;
alphabet.append(1, letter);
random_alphabet.append(1, letter);
}


random_shuffle(random_alphabet.begin(), random_alphabet.end());

int random_len = random_alphabet.length();

int alphabet_len = alphabet.length(), text_len = new_text.length();

criptotext.append(new_text);



for (i=0;i<alphabet_len; ++i){

for(j = 0; j<text_len; ++j){

if(alphabet.at(i) == new_text[j]){

criptotext.replace(j, 1, 1, random_alphabet.at(i));

new_text.replace(j, 1, 1, '*');

}

}

}

cripto_len = criptotext.length();

criptotext[cripto_len] = '\0';

criptotext_ret = criptotext.c_str();

return criptotext_ret;
};
void decrypt(fstream& istream, fstream& ostream, string filterFile, string criptoFile){
int filtered_len, alphabet_len, cripto_len, f_len, nr_ap;
int i, j;
char letter;

const char* filtered_text;
const char* cripto_text;

filtered_len = checkLength(filterFile); //here I get the error and everytime //I try to do smth with the filteredText

filtered_text = new (nothrow) char [filtered_len + 1];

if (filtered_text==0) cout << "Error: null pointer!\n";

cripto_text = new (nothrow) char [filtered_len + 1];

if (cripto_text==0) cout << "Error: null pointer!\n";

const char* file;

file = new (nothrow) char [filtered_len + 1];

if (file==0) cout << "Error: null pointer!\n";

double frecq_txt; //the frecq from a letter in cryptotext;
//the letters frecq
double letter_frecq[26] = {8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.094, 6.966, 0.153, 0.772, 4.025, 2.406, 6.749, 7.507, 1.929,
0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 };

double alpha_frecq[NR_L][NR_C];//it contains the letters and the alphabet frecq

double cripto_frecq[NR_L][NR_C];//it contains the letters and the cryptotext frecq
string alphabet, criptoString;

j = 65;

//introducerea literelor(ASCII) in matricea apha_frecq
for(i = 0; i<NR_C; i++){

alpha_frecq[0][i] = j;

j++;

}

j = 65;


for(i = 0; i<NR_C; i++){

cripto_frecq[0][i] = j;

j++;

}



filtered_text = filterText(istream, filterFile);

cout<<filtered_text<<endl;

f_len = strlen(filtered_text);


ostream.write(filtered_text, f_len);


cripto_text = permutation(istream, criptoFile);


for(i = 65; i<=90; i++){

letter = (char)i;

alphabet.append(1, letter);

}
cripto_len = strlen(cripto_text);


criptoString.append(cripto_text, 0, cripto_len);

alphabet_len = alphabet.length();

nr_ap = 0;//


//calcularea frecventelor din textul criptat
for(i = 0; i<alphabet_len; i++){

nr_ap = 0;

for(j = 0; j<cripto_len; j++){

if(alphabet[i] == criptoString[j]){

nr_ap++;

}

}

frecq_txt = (double)nr_ap/(double)cripto_len;

cripto_frecq[1][i] = frecq_txt;
}


for(i = 0; i<NR_L; i++){

for(j = 0; j<NR_C; j++){

cout<<cripto_frecq[i][j]<<" ";

}
cout<<endl;
}
}
int main(){

fstream istream, ostream;

fstream stream;

decrypt(istream, ostream, "fisier.txt", "cripto.txt");

return 0;
}
In loadFile you don't need that dynamic allocation stream.open(str.c_str(), ios::in|ios::out);

You seem to misunderstand how pointers work. They store a memory address.
1
2
filtered_text = new (nothrow) char [filtered_len+1]; //you reserve memory and its address is in filtered_text
filtered_text = text.c_str(); //you change the pointer, now you lose the reference. If you want to copy the content use strcpy() 


Try to avoid all the char * and use string instead. Check for memory leaks (there isn't a single delete)
Hi,
Thanks for your reply,
I tried to use delete, but I didn't know exactly where to put it cause I couldn't use it in the function(before returning the variable)... Can you tell me when and where I have to free the memory?
About the strcpy(strcnpy) method, I can;t use it because my char* is a const one.... and it gives me errors.
But thanks for telling me how c_str works. It will help me a lot in the future.
You need to delete the memory, when you stop using it and before you override the pointer.
About the strcpy(strcnpy) method, I can;t use it because my char* is a const one....
then don't make it a const one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const char* filterText(fstream& stream, string str){
  char* filtered_text;
  filtered_text = new (nothrow) char [filtered_len+1];
//...
  strcpy( filtered_text, text.c_str() ); //you want to copy the content
  return filtered_text;
}

const char* permutation(fstream& stream, string str){
  const char* filtered_text;
//  filtered_text = new (nothrow) char [filtered_len+1];   //you don't need to reserve here 
  filtered_text = filterText(stream, str); //because you receive the address from the function
  //you use filtered_text
  delete [] filtered_text; //freeing the memory
}


But you could avoid all that pointer problem, if you use strings
Topic archived. No new replies allowed.