[英]Reading RAW audio file
I am a beginner in C++ and I have a school project implying analysing an audio file and I can't read the file, in the example the program keeps reading the first value from the file 我是C ++的初学者,我有一个学校项目暗示要分析音频文件,但无法读取该文件,在该示例中,程序始终从文件中读取第一个值
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream c;
c.open("cv.RAW");
int b;
while(1)
{
c>>b;
cout<<b<<" ";
}
return 0;
}
you need to 你需要
ifstream c("cv.RAW", std::ios_base::binary)
, if you read a binary file without this flag it will be treated as a text file and will give you incorrect values. 以二进制模式打开文件ifstream c("cv.RAW", std::ios_base::binary)
,如果您读取没有此标志的二进制文件,它将被视为文本文件,并为您提供不正确的值。 if (c.good())
or just if (c)
before reading any values 通过在读取任何值之前检查if (c.good())
还是if (c)
来检查文件是否打开 if (c.good())
or just if (c)
after reading each byte 通过读取每个字节后再次检查if (c.good())
还是if (c)
检查读取是否成功 c.read((char*)&b, sizeof(b))
if you want to read an integer at a time, for a single byte use char ch; c.read(&ch, sizeof(ch))
如果您想一次读取一个整数,请使用c.read((char*)&b, sizeof(b))
进行读取,对于单个字节,请使用char ch; c.read(&ch, sizeof(ch))
char ch; c.read(&ch, sizeof(ch))
iostreams don't throw exceptions by default so you always need to check their state every time you use them. iostream默认情况下不会抛出异常,因此每次使用它们时,您始终需要检查它们的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.