重新构建数组以存储在向量中

时间:2012-09-09 11:10:34

标签: c++ arrays

我遇到了一个奇怪的问题。我有一个数组,我需要返回另一个函数。问题是它必须作为

输出/转换
(unsigned)(unsigned char)

处理数据的函数是char *类型。这是我转换数据的函数:

char* 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);

for(unsigned i=0; (i < 20); i++)
{
    //cout << (unsigned)(unsigned char)data[i] << endl; 

}

return (unsigned)(unsigned char) data;
}

在另一个功能中,我有这个:

unsigned char* data = this->readHeader(file);

谁能看到我出错的地方?

谢谢:)

1 个答案:

答案 0 :(得分:1)

this->data = new char[this->dataSize];

因此,据推测,this->data的类型为char*。好的:这也是该功能的返回类型。所以return data似乎是正确的代码。