我必须将输入文件 in.wav 的前 64 个字节复制到输出文件 out.wav 中。 (我下载了一个显示 .wav 文件头( block )的程序:data_subchunk 的前 44 个字节和前 20 个字节)
我的代码用一些值填充了 .wav 文件,但(我确信)它是一个垃圾。 (程序 显示的值不匹配。)
我必须将 in.wav 文件的一部分复制到 out.wav 中:
#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;
struct HEADER
{
char ChunkId[4];
int ChunkSize;
char Format[4];
fmt S1;
data S2;
} header;
int main()
{
FILE *input = fopen("in.wav", "r");
FILE *output = fopen("out.wav", "w");
if(input == NULL)
{
printf("Unable to open wave file\n");
exit(EXIT_FAILURE);
}
fwrite(&input, sizeof(int), 16, output); // 16'ints' * 4 = 64 bytes
fclose(input);
fclose(output);
return 0;
}
怎么了?
最佳答案
您正在将数据从输入写入打开的输出文件
fwrite(&input, sizeof(int), 16, output);
我不确定,如果你可以这样使用 FILE 指针。我会这样做
unsigned char buf[64];
fread(&buf, sizeof(char), 64, input);
fwrite(&buf, sizeof(char), 64, output);
同样以二进制模式打开文件:fopen("in.wav", "rb")
和 fopen("out.wav", "wb")
关于无法将一个 .wav 的开头(前 64 个字节)复制到一个空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398042/