所以在这段代码中,im要做的是从一个wave文件中读取初始的44个字节,然后用一个左右短路的序列计算组合
新文件:(初始44字节-组合-组合-组合…)但当我尝试调用fwrite时它会停止。

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

int main(int argc, char *argv[]) {
    FILE * firstFile;
    FILE * finalFile;
    size_t size;
    char *buffer;
    short left;
    short right;
    short combination;
    int loop;

    printf("%s", argv[1]);
    if ((firstFile = fopen("test.wav", "rb")) == NULL) {
        printf("File error");
        exit(1);
    }

    fseek(firstFile, 0, SEEK_END);

    size = ftell(firstFile);

    fseek(firstFile, 0, SEEK_SET);

    buffer = malloc(44);
    fread(buffer, 1, 44, firstFile);

    finalFile = fopen("new.wav", "rb");
    fwrite(&buffer, sizeof(buffer), 1, finalFile);
    loop = 1;

    while (loop == 1) {
        fread(&left, sizeof(short), 1, firstFile);
        if (fread(&right, sizeof(short), 1, firstFile) == 0) {
            loop = 0;
        }

        combination = (left - right) / 2;
        fwrite(&combination, sizeof(short), 1, finalFile);
    }

    fclose(firstFile);
    fclose(finalFile);
    free(buffer);

    return 0;
}

最佳答案

你刚刚以“rb”模式打开了写文件。

finalFile = fopen("new.wav", "rb");

请试试看
finalFile = fopen("new.wav", "wb");

然后检查
finalFile==NULL

10-04 18:01