fread上的段错误

时间:2013-11-06 01:55:48

标签: c segmentation-fault fread

我正在尝试读取.au文件的标题(前24个字节,分为6个uint32_t)并打印出编码,采样率和通道数。最终我将播放.au文件的其余部分,但是现在当我到达fread线时(我通过gdb检查),我仍然坚持获得段错误。为什么会这样?我的代码如下。感谢。

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <alsa/asoundlib.h>
#include <fcntl.h>

#define AUDIO_FILE_MAGIC (uint32_t)0x02E736E64

#define AUDIO_FILE_ENCODING_MULAW 1
#define AUDIO_FILE_ENCODING_LINEAR_8 2
#define AUDIO_FILE_ENCODING_LINEAR_16 3

#define STEREO 2
#define MONO 1

typedef struct {
    uint32_t magic;
    uint32_t hdr_size;
    uint32_t data_size;
    uint32_t encoding;
    uint32_t sample_rate;
    uint32_t channels;
} au_header;

int main(int argc, char **argv) {   
    // Check arguments
    if(argc != 2) {
        fprintf(stderr, "Usage: %s <filename.au>\n", argv[0]);
        exit(1);
    }
    au_header header;
    char str[32];
    FILE *f = fopen(argv[1], "rb");

    fread(str, 4, 6, f);
    fclose(f);
    uint32_t *intptr = (uint32_t *) str;

    // Remember to change from network order
    header.magic = ntohl(intptr[0]);
    header.hdr_size = ntohl(intptr[1]);
    header.data_size = ntohl(intptr[2]);
    header.encoding = ntohl(intptr[3]);
    header.sample_rate = ntohl(intptr[4]);
    header.channels = ntohl(intptr[5]);

    printf("endcoding: %d\nsample rate: %d\nchannels: %d\n", header.encoding, header.sample_rate, header.channels);
    /* TO DO:

       Implement audio file playback here

    */

}

0 个答案:

没有答案