I’m trying to read a short from a binary file, but I end up with a lot of 0’s
This is the write function
void AudioBuffer::WriteToFile(const string& strFilename)
{
fstream fout(strFilename.c_str(), ios::out|ios::binary);
short sample;
for (VECTOR_SHORT_ITER iter = m_vectorSamples.begin(); iter != m_vectorSamples.end(); iter++)
{
sample = (short) *iter;
fout.write((char *) &sample, sizeof(short));
}
fout.close();
}
And this is what I’ve got for the reading function, I’m aware of the possible overflow with atoi
void AudioBuffer::FileToBuffer(const string& strFilename)
{
fstream fin(strFilename.c_str(), ios::in|ios::binary);
short iSample;
char *temp = new char[sizeof(short)];
cout<<"Samples Output"<<endl;
while(!fin.eof())
{
fin.read(temp,sizeof(short));
iSample = atoi(temp);
cout<<iSample< " ";
m_vectorSamples.push_back(iSample);
*temp = NULL;
}
fin.close();
}
Also, clearing the char pointer by doing the *temp = NULL isn’t the best thing right?
Thanks
Since you’re just writing raw bits into the file, you want to read the same way, something on this order: