入力ファイルの先頭が最初の if ステートメントと一致するのに、なぜこれが別のファイルを出力するのかわかりません。また、複数の検索結果がある場合、次のファイル名は、最後のファイル名に新しいファイル名を加えたものになります。
int main()
{
unsigned short n(0);
unsigned char y;
std::ostringstream filename;
std::ifstream input("data.dat", std::ios::binary);
input >> std::noskipws >> std::hex;
std::ofstream output;
output << std::ios::binary;
while (input.good()) {
filename.clear();
filename << n << ".dat";
output.open(filename.str());
while ( input.good() )
{
input >> y;
output << y;
if ( y == 0xFF ) {
input >> y;
output << y;
if ( y == 0xD9 ) {
input >> y;
output << y;
output.close();
break;
}
}
}
if (output.is_open())
output.close();
++n;
}
return 0;
}