读取WAV文件将返回0个字节

问题描述

所以我正在读取.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方法。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...