问题描述
所以我正在读取.wav文件,但它返回0个字节。
FILE *pFile = fopen("file.wav","r");
if (pFile == nullptr)
{
cout << "Unable to open wave file";
return 1;
}
long lSize = ftell (pFile);
char *p = new char[lSize];
size_t bytesRead = fread(p,1,lSize,pFile); // 0 bytes ```
解决方法
在获取文件位置之前,您需要先搜索到末尾,然后快退:
FILE *pFile = fopen("file.wav","rb");
if (pFile == nullptr)
{
cout << "Unable to open wave file";
return 1;
}
fseek(pFile,SEEK_END); // <----- ADD THIS -------<
long lSize = ftell (pFile);
rewind(pFile); // <----- AND THIS -------<
char *p = new char[lSize];
size_t bytesRead = fread(p,1,lSize,pFile);
确保以只读二进制文件打开。 “ rb”-看fopen
方法。