Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:WAV文件未在“中打开”;rb";_C_File_Audio_Binary_Wav - Fatal编程技术网

C:WAV文件未在“中打开”;rb";

C:WAV文件未在“中打开”;rb";,c,file,audio,binary,wav,C,File,Audio,Binary,Wav,我很难打开WAV文件进行读取。当我编译和运行我的代码时,我没有得到任何错误。我正在添加我正在处理的代码(假设调用了必要的库)。该程序应该显示WAV文件的内容,尽管我输入了有效的文件名和扩展名,但“无效文件名。请重试”语句仍会打印到屏幕上。我尝试的另一种方法是输入文件的目录,而不仅仅是名称,当我这样做时,我的程序结束,没有显示任何内容。任何指导都会很有帮助,并提前感谢您 main(){ FILE *fin; printf("\nEnter filename of WAV file: \n"); c

我很难打开WAV文件进行读取。当我编译和运行我的代码时,我没有得到任何错误。我正在添加我正在处理的代码(假设调用了必要的库)。该程序应该显示WAV文件的内容,尽管我输入了有效的文件名和扩展名,但“无效文件名。请重试”语句仍会打印到屏幕上。我尝试的另一种方法是输入文件的目录,而不仅仅是名称,当我这样做时,我的程序结束,没有显示任何内容。任何指导都会很有帮助,并提前感谢您

main(){
FILE *fin;
printf("\nEnter filename of WAV file: \n");
char filename[256];
scanf("%s",&filename);
fin = fopen(filename,"rb"); // opens in rb

if(!fin) // if file doesn't exist
{
printf("Invalid filename. Try again.\n");
}
else // if fin opens succesfully
{
    printf("\nFile opened succesfully\n");

    char *header;
    header = (char *)malloc(44);
    if(header == NULL)
    {
        printf("Error in allocating memory.");
        return 0;
    }


    fread(header,1,44,fin);

    char *chunkid;
    unsigned int *chunksize;
    char *format;
    char *subchunk1id;
    unsigned int *subchunk1size;
    unsigned short int *audioformat;
    unsigned short int *numchannels;
    unsigned int *samplerate;
    unsigned int *byterate;
    unsigned short int *blockalign;
    unsigned short int *bitspersample;
    char *subchunk2id;
    unsigned int *subchunk2size;
    unsigned int *data;

    chunkid = header;
    chunksize = (unsigned int *)(header + 4);
    format = header + 8;
    subchunk1id = header + 12;
    subchunk1size = (unsigned int *)(header + 16);
    audioformat = (unsigned short int *)(header + 18);
    numchannels = (unsigned short int*)(header + 20);
    samplerate = (unsigned int*)(header + 24);
    byterate = (unsigned int*)(header + 28);
    blockalign = (unsigned short int*)(header + 30);
    bitspersample = (unsigned short int*)(header + 32);
    subchunk2id = header + 36;
    subchunk2size = (unsigned int*)(header + 40);
    data = (unsigned int*)(header + 44);

    printf("\n%c%c%c%c",*(header),*(header+1),*(header+2),*(header+3));
    printf("\n%d",*chunksize);
    printf("\n%c%c%c%c",*(header + 8),*(header + 9), *(header + 10), *(header + 11));
    printf("\n%c%c%c%c",*(header + 12),*(header + 13), *(header + 14), *(header + 15));
    printf("\n%d",*subchunk1size);
    printf("\n%d",*audioformat);
    printf("\n%d",*numchannels);
    printf("\n%d",*samplerate);
    printf("\n%d",*byterate);
    printf("\n%d",*blockalign);
    printf("\n%d",*bitspersample);
    printf("\n%c%c%c%c",*(header + 36),*(header + 37),*(header + 38), *(header + 39));
    printf("\n%d",*subchunk2size);
    printf("\n%d",*data);


    fclose(fin);

} // end of else
} // end of main
试用

if(!fin)
{
  perror("Error:");
}
它将打印正确的错误消息,以便您轻松诊断无法打开文件的原因。可能是您的程序找不到您指定的文件,或者您没有打开该文件的权限。”s打开文件时可能遇到的错误列表

我看到的另一个可能的问题是,您正在使用
scanf
将stdin读取到char数组中。如果您指定的路径包含空格怎么办?每个单词后跟一个空格将被视为不同的字符串。你只会得到第一个不包含空格的最长单词。因此,您的程序没有读取整个路径。你可以试试
scanf(“%[^\n]”,文件名)
将一直读取,直到遇到
'\n'
。当心!这使您面临缓冲区溢出的可能性,即如果您将长度大于256的有效字符串集传递给程序,您将写入超出字符数组边界的内容,这是不好的。因此,请尝试使用
scanf(“%255[^\n]”,文件名)
或尝试使用提供缓冲区溢出保护的
fgets()

在编译代码时使用警告会暴露您的错误

改进建议,将标题描述为类似以下结构:

typedef struct  
{
    char    riff_tag[4];
    int32_t riff_length;
    char    wave_tag[4];
    char    fmt_tag[4];
    int32_t fmt_length;
    int16_t audio_format;
    int16_t num_channels;
    int32_t sample_rate;
    int32_t byte_rate;
    int16_t block_align;
    int16_t bits_per_sample;
    char    data_tag[4];
    int32_t data_length;
}waveFileHeader, *waveFileHeaderptr ;
读起来就像:

waveFileHeaderptr header =malloc ( sizeof (waveFileHeader));
fread (header , sizeof (waveFileHeader), 1 , fin);

将使下面的代码和处理变得不那么凌乱

您的右大括号(
}
)至少比右大括号(
{
)多两个。不确定您使用的编译器是什么,但
gcc
在您的
scanf
行上报告了一条警告:
警告:格式“%s”要求参数的类型为“char*”,但参数2的类型为“char(*)[256]”[-Wformat=]scanf(“%s”,&filename);
哦,我明白了。将
scanf(“%255s”,&filename);
更改为
scanf(“%255s”,filename);
。您需要将字符串持有者的第一个字节的地址作为参数发送到
scanf()
,而不是指向此地址的指针。
numchannels=(unsigned short int*)(header+20);
违反。您无法确保
header+20
unsigned short
的有效地址“它在我的系统上工作”不会改变它是未定义行为的事实。您使用的是什么操作系统和编译器?问题是它找不到文件。这帮助我解决了这个问题,谢谢!