C,无法创建文件/向文件写入信息

问题描述 投票:0回答:2

我正在制作一个应该反转 .wav 音频文件的程序,所以我做到了,但无论如何它似乎都没有创建任何

output.wav

我认为问题是我尝试将信息写入文件的地方,也许我再次搞乱了内存。

代码:

WavHeader.h:

#include <stdint.h>
typedef uint8_t   BYTE;
typedef uint16_t  WORD;
typedef uint32_t  DWORD;
typedef struct
{
    BYTE   chunkID[4];
    DWORD  chunkSize;
    BYTE   format[4];
    BYTE   subchunk1ID[4];
    DWORD  subchunk1Size;
    WORD   audioFormat;
    WORD   numChannels;
    DWORD  sampleRate;
    DWORD  byteRate;
    WORD   blockAlign;
    WORD   bitsPerSample;
    BYTE   subchunk2ID[4];
    DWORD  subchunk2Size;
} __attribute__((__packed__))
WAVHEADER;

反向.c:

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

#include "wav.h"

// Number of bytes in .wav header
const int HEADER_SIZE = 44;

int check_format(WAVHEADER header);
int get_block_size(WAVHEADER header);

int main(int argc, char *argv[])
{
    // Ensure proper usage
    if (argc != 3)
    {
        printf("Usage: ./reverse input.wav output.wav\n");
        return 1;
    }
    // Open input file for reading
    FILE *input = fopen(argv[1], "rb");
    if (input == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    // Read header
    WAVHEADER header;
    fread(&header, 1, HEADER_SIZE, input);

    // Use check_format to ensure WAV format
    int result = check_format(header);
    if (result == 0)
    {
        return 0; // format is okay
    }
    if (result != 0)
    {
        printf("File is unsupported format, ensure it extension is a .WAV\n");
        return 1;
    }

    // Open output file for writing
    FILE *output = fopen(argv[2], "w");
    if (output == NULL)
    {
        fclose(input);
        printf("Could not create file.\n");
        return 1;
    }

    // Write header to file
    fwrite(&header, 1, HEADER_SIZE, output);

    // Use get_block_size to calculate size of block
    int block =  get_block_size(header);


    // Write reversed audio to file

    int16_t *buffer = malloc(block); // Allocate memory for the audio buffer
    if (buffer == NULL)
    {
        printf("Memory allocation error.\n");
        fclose(input);
        fclose(output);
        return 1;
    }

    fseek(input, HEADER_SIZE, SEEK_SET);

    // Calculate the number of audio blocks in the file
    long numBlocks = (header.subchunk2Size) / block;

    // Read audio blocks from input, reverse, and write to output
    for (long i = 0; i < numBlocks; i++)
    {
        fseek(input, -(i + 1) * block, SEEK_END);
        fread(buffer, block, 1, input);
        fwrite(buffer, block, 1, output);
    }

    // clean up
    free(buffer);
    fclose(input);
    fclose(output);
    return 0;
}

int check_format(WAVHEADER header)
{
    if (header.format[0] == 'W' && header.format[1] == 'A' && header.format[2] == 'V' && header.format[3] == 'E')
    {
        return 0; // if format is correct
    }
    else
    return 1; // if format is not .WAV
}

int get_block_size(WAVHEADER header)
{
    int blockSize = header.numChannels * (header.bitsPerSample / 8);
    return blockSize;
}

我尝试过

errno
和少量调试,但没有任何帮助。期待看到
"Couldn't create file"
,但没有,所以我认为问题在于将信息写入文件。

c file pointers memory functional-programming
2个回答
0
投票

我认为问题是我尝试将信息写入文件的位置

不,问题就在这里:

int result = check_format(header);
if (result == 0)
{
    return 0; // format is okay
}
if (result != 0)
{
    printf("File is unsupported format, ensure it extension is a .WAV\n");
    return 1;
}
...
// NEVER REACHED

0
投票

由于以下原因,代码永远无法到达打开输出文件的部分:

    if (result == 0)
    {
        return 0; // format is okay
    }
    if (result != 0)
    {
        printf("File is unsupported format, ensure it extension is a .WAV\n");
        return 1;
    }

如果

result
0
,它应该继续,但它会立即返回
0
,退出该函数。

只需删除第一个

if
语句,只留下错误检查,所以看起来像:

    if (result != 0)
    {
        printf("File is unsupported format, ensure it extension is a .WAV\n");
        return 1;
    }

这样,当

result
0
时,它将继续执行其余代码。

© www.soinside.com 2019 - 2024. All rights reserved.