M
meyousikmann
I am having a little trouble with dynamic memory allocation. I am trying to
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic arrays.
I don't know the size of the text file ahead of time, though, so I created a
class that includes a method to resize the array. Here is that class:
class Data
{
public:
Data(int initialsize);
~Data();
char **array;
void resize(int newsize);
void add(const int index, const string temp);
private:
int size;
int high;
int low;
};
Data:
ata(int initialsize)
{
size = initialsize;
array = new char*[size];
}
Data::~Data()
{
delete array;
}
void Data::resize(int newsize)
{
char **newarray;
newarray = new char*[newsize];
memcpy(newarray, array, size*sizeof (char*));
size = newsize;
delete array;
array = newarray;
}
Now, here is my driver program where I am reading the data and trying to put
into the array from the Data class above:
#include <iostream>
#include <fstream>
#include <string>
#include "Data.h"
using namespace std;
int main()
{
char filename[14];
ifstream infile;
string temp;
cout << "Please input filename: ";
cin >> filename;
cout << endl;
infile.open(filename);
if(infile.fail())
cout << "Invalid filename." << endl;
else
{
int size = 0; // initial size of data array (zero because we don't yet
know how many lines there are)
Data myData(size); // create a data array object
// keep reading lines from the data file until end of file is reached
while(!infile.eof())
{
myData.resize(++size); // add space to the array object for another data
line
getline(infile, temp, '\n');
strcpy(myData.array[size - 1], temp.c_str());
}
}
return 0;
}
The data file is simply a multiline text file with each line terminated with
a CRLF.
Everything compiles, but when I go to run it, I get a message that says
"Unhandled exception blah blah blah" when the strcpy(myData.array[0],
temp.c_str()); line is executed.
Can anyone help me figure out how to make this work?
Thanks
read a text file and put the contents into a dynamic array. I know I can
use vectors to make this easier, but it has to be done using dynamic arrays.
I don't know the size of the text file ahead of time, though, so I created a
class that includes a method to resize the array. Here is that class:
class Data
{
public:
Data(int initialsize);
~Data();
char **array;
void resize(int newsize);
void add(const int index, const string temp);
private:
int size;
int high;
int low;
};
Data:
{
size = initialsize;
array = new char*[size];
}
Data::~Data()
{
delete array;
}
void Data::resize(int newsize)
{
char **newarray;
newarray = new char*[newsize];
memcpy(newarray, array, size*sizeof (char*));
size = newsize;
delete array;
array = newarray;
}
Now, here is my driver program where I am reading the data and trying to put
into the array from the Data class above:
#include <iostream>
#include <fstream>
#include <string>
#include "Data.h"
using namespace std;
int main()
{
char filename[14];
ifstream infile;
string temp;
cout << "Please input filename: ";
cin >> filename;
cout << endl;
infile.open(filename);
if(infile.fail())
cout << "Invalid filename." << endl;
else
{
int size = 0; // initial size of data array (zero because we don't yet
know how many lines there are)
Data myData(size); // create a data array object
// keep reading lines from the data file until end of file is reached
while(!infile.eof())
{
myData.resize(++size); // add space to the array object for another data
line
getline(infile, temp, '\n');
strcpy(myData.array[size - 1], temp.c_str());
}
}
return 0;
}
The data file is simply a multiline text file with each line terminated with
a CRLF.
Everything compiles, but when I go to run it, I get a message that says
"Unhandled exception blah blah blah" when the strcpy(myData.array[0],
temp.c_str()); line is executed.
Can anyone help me figure out how to make this work?
Thanks