Reputation: 2674
So I know I have asked this question before, however, I am still stuck and before I can move on with my project. Basically, I'm trying to read in a .wav file, I have read in all of the required header information and then stored all the data inside a char array. This is all good, however, I then recast the data as an integer and try and output the data.
I have tested the data in MatLab, however, I get very different results:
Matlab -0.0078
C++: 1031127695
Now these are very wrong results, and someone kindly from here said it's because I'm outputting it as an integer, however, I have tried pretty much every single data type and still get the wrong results. Someone has suggestion that it could be something to do with Endianness (http://en.wikipedia.org/wiki/Endianness) .. Does this seem logical?
Here is the code:
bool Wav::readHeader(ifstream &file)
{
file.read(this->chunkId, 4);
file.read(reinterpret_cast<char*>(&this->chunkSize), 4);
file.read(this->format, 4);
file.read(this->formatId, 4);
file.read(reinterpret_cast<char*>(&this->formatSize), 4);
file.read(reinterpret_cast<char*>(&this->format2), 2);
file.read(reinterpret_cast<char*>(&this->numChannels), 2);
file.read(reinterpret_cast<char*>(&this->sampleRate), 4);
file.read(reinterpret_cast<char*>(&this->byteRate), 4);
file.read(reinterpret_cast<char*>(&this->align), 2);
file.read(reinterpret_cast<char*>(&this->bitsPerSample), 4);
char testing[4] = {0};
int testingSize = 0;
while(file.read(testing, 4) && (testing[0] != 'd' ||
testing[1] != 'a' ||
testing[2] != 't' ||
testing[3] != 'a'))
{
file.read(reinterpret_cast<char*>(&testingSize), 4);
file.seekg(testingSize, std::ios_base::cur);
}
this->dataId[0] = testing[0];
this->dataId[1] = testing[1];
this->dataId[2] = testing[2];
this->dataId[3] = testing[3];
file.read(reinterpret_cast<char*>(&this->dataSize), 4);
this->data = new char[this->dataSize];
file.read(data, this->dataSize);
unsigned int *te;
te = reinterpret_cast<int*>(&this->data);
cout << te[3];
return true;
}
Any help would be really appreciated. I hope I've given enough details.
Thank you.
Upvotes: 0
Views: 220
Reputation: 138
I think there're several issues with your code. One of them are the casts, for instance this one:
unsigned int *te;
te = reinterpret_cast<int*>(&this->data); // (2)
cout << te[3];
te is a pointer to unsigned int, while you try to cast to a pointer to int. I would expect compilation error at line (2)...
And what do you mean by te[3]? I expect some garbage from *(te + 3) memory location to be outputed here.
Upvotes: 1