hi all,
I have a binary file, then I try to read them and show the value to the screen, but what I got is always -13108. I don't know why. Here is my code

#include <iostream>
#include <fstream>
using namespace std;

void main()
{
	std::fstream f_in, f_out;
	short speech, value[1000];
	

	f_in.open ("myfile.pcm", std::ios::in | std::ios::binary);
	f_out.open ("test2.org", ios::out | ios::binary);

	int i = 0;
	while (i < 100) {
		f_in.read((char *)&speech, 2);
		value[i] = speech;
		cout << value[i] << std::endl;
		f_out.write((char *)&speech, 2);

		i++;
	}
}

Thks for your help

are you getting the same number for every value?
are you sure that the size of each element is 2?

yes, I always get -13108 for every value.
And yes, because my binary file is actually a raw pcm file, with short 16-bit for each value.
thks

have you tried adding a cout statement before you assign the value to the array to make sure you are getting an assignment.

f_in.read((char *)&speech, 2);
cout << value[i] << "\t";  // show the value before assignment
value[i] = speech;
cout << value[i] << std::endl;
f_out.write((char *)&speech, 2);

Is the input file opened? Is the read successful? Some error checking might help.

commented: A guess is all that is needed at times. +16

you're right, they are -13108 too.
I wonder what's wrong with my code :-(
thks for ur help.

Is the input file opened? Is the read successful? Some error checking might help.

thks, it haven't been opened yet. now the problem is solved.
thks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.