【发布时间】:2014-10-28 19:03:54
【问题描述】:
通常我生活在我的 C# 世界中。但有时我必须爆发并在外面做点什么。
目前我必须解码音频流,并且必须直接在我的 c++ 控制台应用程序中输出。
如果我将内容写入文件,我可以听到正确的结果。 但如果我使用 fstream cout 而不是,我只会听到嘈杂的声音。
我必须如何正确地做到这一点? 这里是工作文件流代码:
fstream wavefile;
wavefile.open(output, ios::out | ios::binary | ios::trunc);
//do something
wavefile.write((char*) &waveheader, waveheadersize);
//do something else
do {
//do something
//decodedBuffer is of type BYTE* , decodedLength is of type DWORD
wavefile.write((char*) decodedBuffer, decodedLength);
wavefile.flush();
} while (encodedLength > 0);
我不工作的 cout 代码:
std::cout.setf(ios::out | ios::binary | ios::trunc);
//do something
//this works, I got the same output
cout << structToString(&waveheader) << endl;
//do something else
do {
//do something
cout << (char *)decodedBuffer;
} while (encodedLength > 0);
提前致谢
【问题讨论】:
-
使用
std::cout时将输出重定向到文件并进行比较 -
ios::out、ios::binary和ios::trunc都不是格式化标志。见en.cppreference.com/w/cpp/io/ios_base/setf -
您需要包含实际的错误消息,以使其成为有效问题。 “无效的代码”太含糊了。
-
让我猜猜,您在 Windows 上,因此您的流上的文本模式翻译正在扼杀您的数据。
-
即使是这样,
cout << (char *)decodedBuffer也会调用格式化的输出插入器。你知道std::cout仍然有一个write方法,对吧?它的行为类似于unformatted output function,这可能是您想要的(实际上在使用std::basic_ostream::write的std::fstream写入操作中使用)。