Код иногда, а иногда он полностью падает на меня. Я подозреваю, что это происходит потому, что мой массив указателей, вероятно, указывает на какое-то случайное место в памяти. Я считаю, что установка места так, чтобы все элементы указателя указывали на составное место.
При чтении из файла с небольшим количеством слов он работает отлично. Однако, как только я читаю из огромного файла, в мою программу попадает много мусора. Спасибо!
#include <iostream>
#include <fstream>
using namespace std;
class orignialData {
char* data;
public:
void setData(char* s) { data = strdup(s);}
char* getData() const {return data;}
};
class dataClass {
orignialData** W_;
unsigned int capacity_; // max number of words Dictionary can hold
unsigned int numberOfWordsInDictionary_;
public:
dataClass(char* filename);
void addData();
void viewAll() const;
void reSize();
};
void dataClass::reSize() {
static int i = 0;
W_[i] = new orignialData;
if (i == 7) {
cout << "Dictionary is now the size of: " << i << endl;
}
i++;
}
dataClass::dataClass(char* filename) {
fstream file;
char buff[30];
capacity_ = 8;
file.open(filename, ios::in);
if(file.fail()) {
cout << "There was an error oppennig the file....\n";
}
W_ = new orignialData*[capacity_];
while (file >> buff) {
static int i = 0;
reSize();
W_[i] -> setData(buff);
i++;
}
file.close();
};
void dataClass::viewAll() const {
cout << W_[0] -> getData() << endl;
cout << W_[1] -> getData() << endl;
cout << W_[2] -> getData() << endl;
cout << W_[3] -> getData() << endl;
cout << W_[4] -> getData() << endl;
cout << W_[5] -> getData() << endl;
cout << W_[6] -> getData() << endl;
cout << W_[7] -> getData() << endl;
}
int main() {
dataClass dic("simple");
cout << "calling the view all funcion....\n";
dic.viewAll();
return 0;
}