-1

我想用文件hdr中的数据填充(结构)变量,in.wav并且我想将文件的前 64 个字节复制in.wav到另一个文件(out.wav)。

但!第二次使用时,从第一次使用结束的地方fread()开始复制。为什么?in.wavfread()

#include <stdio.h>
#include <stdlib.h>

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]; // 10 secs of garbage. he-he)
} 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");
    FILE *output = fopen("out.wav", "wb");

    unsigned char buf[64];
    header hdr;

    if(input == NULL)
    {
        printf("Unable to open wave file\n");
        exit(EXIT_FAILURE);
    }

    fread(&hdr, sizeof(char), 64, input);


    fread(&buf, sizeof(char), 64, input);
    fwrite(&buf, sizeof(char), 64, output);


    printf("\n>>> %4.4s", hdr.ChunkID);

    fclose(input);
    fclose(output);

    return 0;
}

有什么事?

4

2 回答 2

5

这是有意的。fread始终从文件的当前读取指针读取并推进相同的指针,因此您可以按顺序块中的文件而无需显式查找。

您不必连续两次读取相同的块。您以这种方式检查的是其他进程是否同时更改了文件,如果有,那么您的程序将错误地报告复制失败。

于 2013-05-06T14:16:57.963 回答
2

The file pointer is moved. Read more about it here: Does fread move the file pointer? . You can use fseek or rewind to position at the beginning of a file.

于 2013-05-06T14:17:26.667 回答