c语言 - 应用程序崩溃,虽然我不明白原因



输入文件为in.wav。我必须读取块(成功)并读取样本以规范化音频文件…

问题是,它崩溃,而试图找到。wav文件的样本的maxmin值。它只会在数组中找到最小值和最大值,但是它崩溃了……请告诉我出了什么事。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#define hdr_SIZE 64
typedef struct FMT
{
    char        SubChunk1ID[4];
    int         SubChunk1Size;
    short int   AudioFormat;
    short int   NumChannels;
    int         SampleRate;
    int         ByteRate;
    short int   BlockAlign;
    short int   BitsPerSample;
} fmt;
typedef struct DATA
{
    char        Subchunk2ID[4];
    int         Subchunk2Size;
    int         Data[441000]; 
} data;
typedef struct HEADER
{
    char        ChunkID[4];
    int         ChunkSize;
    char        Format[4];
    fmt         S1;
    data        S2;
} header;

int main()
{
    FILE *input = fopen( "in.wav", "rb");   /// nameIn
    if(input == NULL)
    {
        printf("Unable to open wave file (input)n");
        exit(EXIT_FAILURE);
    }
    FILE *output = fopen( "out.wav", "wb"); /// nameOut
    header hdr;

    fread(&hdr, sizeof(char), hdr_SIZE, input); 
    /* NOTE: Chunks has been copied successfully. */

    /*###############################*/                                  
    /*##### UPDATE (char *ptr;) #####*/
    /*###############################*/
    char *ptr;  // 'int' was written here instead of 'char'. That's was a stupid mistake...
    long n = hdr.S2.Subchunk2Size;

    /// COPYING SAMPLES...
    ptr = malloc(sizeof(hdr.S2.Subchunk2Size));
    while ( n-- != 0 )
    {
        fread(&ptr, 1, 1, input);  // Continues reading after the least 'stop' place. 
    }                              // I was being told here (on "stack") that it is so...

    n = hdr.S2.Subchunk2Size; // Resetting 'n'.
    int min = ptr[0], max = ptr[0], i;
    /* THE PROBLEM IS HERE: */
    for ( i = 0; i < n; i++ )
        {
            if ( ptr[i] < min )     // If the next elements is less than previous, swap them.
                min = ptr[i];
            if ( ptr[i] > max ) // If the next elements is bigger than previous, swap them.
                max = ptr[i];
    }
    printf("> > >%d__%dn", min, max);    // Displaying of 'min' and 'max'.
    fclose(input);
    fclose(output);
    return 0;
}

更新:

尤里卡!这都是因为每个样本有8位!我必须像处理char类型一样处理它们(使用样本)。(见我的"# # # # # #"更新代码中的注释)

代码:

    /// COPYING SAMPLES...
    ptr = malloc(sizeof(hdr.S2.Subchunk2Size));
    while ( n-- != 0 )
    {
        fread(&ptr, 1, 1, input);  // Continues reading after the least 'stop' place. 
    }                              // I was being told here (on "stack") that it is so...

覆盖ptr变量n的第一个字节数次。这破坏了ptr的价值。即使您将其修改为读入已分配的缓冲区(通过删除&),您也只会重写已分配内存的第一个字节。

你可能想说的是:

    fread(ptr, 1, n, input);

注意,不需要while循环。但是,这只是我对你真实意图的猜测。

你正在破坏你的malloc()ptr:

fread(&ptr, 1, 1, input); /* overwrite the content of ptr */

,所以程序崩溃,如果它试图使用ptr。用途:

fread(ptr, 1, 1, input);

或者更好:不使用while循环,使用:

  fread(ptr, 1, n, inout);

正如其他人指出的那样,ptr在此块中不加1,

while ( n-- != 0 )
{
    fread(&ptr, 1, 1, input);  // FIXME, ptr not incremented 
}

但是你正在尝试将1字节的数据存储为整数;这就是你想要的吗?

最新更新