Hey guys I'm having a little problem with unsigned char and reading a file of integers into the array.
1. What the program is suppose to do:
It's suppose to dynamically create an array of unsigned char that is the correct size to read in the array of numbers. The program then reads the numbers into the array. This is all taking place in a static library that is separate to the main program. The class returns a pointer to the array in main. (not got a problem with this so far)
2. What the problem is:
My problem is reading in the file into an unsigned char array. I've been told to do it this way but I'd normally use an int array. I'm not sure if I should be typecasting or just reading it in. I've spent hours on this and need to get it out of the way so I can move onto the next part.
The code for the file read function is below.
C1dArray::C1dArray(int size)
{
pArray = new unsigned char *[size];
}
Function to create the unsigned char.
int C1dArray::LoadC1dArray(char *imgName)
{
int i;
int fileSize = 0;
int buffer;
ifstream myStream;
myStream.open(imgName, ios::binary);//opens input stream with given file name.
if(!myStream){//File load error
cout << "Unable to open file with the name " << imgName << endl;
system("pause");
return (0);//exit
}
else {
while(!myStream.eof())
{
myStream >> buffer;//to advance 1 line at a time.
fileSize++;
}
size = fileSize;
myStream.seekg (0, ios::beg);//returns to front of file.
pArray = new unsigned char [size];
for(i=0;i<size;i++)
{
myStream >> pArray[i];
}
}
}
If I do it like this the program breaks I'm not sure if the constructor is getting called properly because the pointer doesn't seem to be fully working. Any help is appreciated and if more code from the program is required then please do ask. Thanks in advance.