cs50 反向 pset 5 结果 1 不返回 1

问题描述 投票:0回答:1
  1. 列出项目
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wav.h"

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

int main(int argc, char *argv[])
{
    // Ensure proper usage
    // TODO #1
    if (argc < 2)
    {
        printf("Invalid usage (CLA!)\n");
        return 1;
    }

    // Open input file for reading
    // TODO #2

    // Set second cla to fname
    char *file_name = argv[1];

    // Open fname in read mode and store in wav
    FILE *wav = fopen(file_name, "r");

    // If wav can't be opened
    if (wav == NULL)
    {
        // Error message
        printf("Input is not a WAV file.\n");
        return 1;
    }

    // Read header
    // TODO #3

    WAVHEADER header; // Wav header

    // Read wav into address of header
    int result = fread(&header, sizeof(WAVHEADER), 1, wav);

    if (result != 1)
    {
        return 1;
    }


    // Use check_format to ensure WAV format
    // TODO #4
    check_format(header);

    // Open output file for writing
    // TODO #5

    // Setting filename to argv[2]
    char *outfile_name = argv[2];

    // Opening file in write mode
    FILE *outfile = fopen(outfile_name, "wb");

    // Check if outfile opened correctly
    if (outfile == NULL)
    {
        printf("Error outfile didn't open correctly!!!\n");
        return 1;
    }

    // Write header to file
    // TODO #6
    fwrite(&header, sizeof(WAVHEADER), 1, outfile);

    // Use get_block_size to calculate size of block
    // TODO #7
    int size_of_block = get_block_size(header);

    // Write reversed audio to file
    // TODO #8

    // Size of audio data
    fseek(wav, 0, SEEK_END); // end of file
    int file_size = ftell(wav); // current file position
    fseek(wav, 0, SEEK_SET); // back to begining of file

    // Checking if wav is open
    if (wav == NULL)
    {
        printf("wav is not opened\n");
    }
    
    // Sample size
    int a = fread(&(header.bitsPerSample), sizeof(header.bitsPerSample), 1, wav);
    // Checking if fread had an error
    if (a != 1)
    {
        printf("fread failed to read");
    }


    int sample_size = header.bitsPerSample / 8;

    int size = file_size - sizeof(WAVHEADER); // minus size of header
    int num_samples = size / sample_size;
    short* reversed = malloc(num_samples * 2 * sizeof(short));

    if (reversed == NULL)
    {
        printf("reversed array was equal to null");
        return 1;
    }

    if (num_samples / 2 != 0)
    {
         num_samples = num_samples - 1;
    }


        int result1 = fread(reversed, sample_size, 2 * num_samples, wav);

        if (result1 != 2 * num_samples)
        {
            printf("RESULT 1 DONT EQUAL 1\n");
            return 1;
        }

    for (int i = 0; i < num_samples / 2; i++)
    {
         // Store the pair of samples at index i
        short temp = reversed[2 * i];
        short temp1 = reversed[2 * i + 1];

        // Swap the pair of samples at index i with the pair at index num_samples / 2 - 1 - i
        reversed[2 * i] = reversed[2 * (num_samples / 2 - 1 - i)];
        reversed[2 * i + 1] = reversed[2 * (num_samples / 2 - 1 - i) + 1];

        // Put the stored samples at index num_samples / 2 - 1 - i
        reversed[2 * (num_samples / 2 - 1 - i)] = temp;
        reversed[2 * (num_samples / 2 - 1 - i) + 1] = temp1;

    }
    int result2 = fwrite(reversed, sample_size, num_samples, outfile);

    // Check if all audio was reversed
    if (result2 < num_samples)
    {
        printf("some audio not reversed");
    }

    // Close files
    fclose(outfile);
    fclose(wav);
    free(reversed);
    }


int check_format(WAVHEADER header)
{
    // TODO #4
    if (strncmp((char *) header.format, "WAVE", 4) == 0)
    {
        return 0;
    }
    else
    {
        printf("NOT A WAV FILE !\n");
        return 1;
    }
}

int get_block_size(WAVHEADER header)
{
    // TODO #7
    long total_channels = header.numChannels;
    long bits_per_sample = header.bitsPerSample;
    long block_size = total_channels * (bits_per_sample / 8);

    return (int) block_size;
}

result1 没有返回 1,所以我假设是 num_samples 或其他东西,如果有人可以帮助我找到我的错误,那将非常有帮助。

我尝试过 ddb 和 cs50.ai 但我似乎找不到问题。

这是错误消息 result1 不返回 1 所以我假设 num_samples 是错误的,但我不确定,我希望有人能看到我的错误

c cs50
1个回答
0
投票

您必须以二进制模式打开二进制文件,以避免在旧平台上出现行尾转换:

    FILE *wav = fopen(file_name, "rb");
© www.soinside.com 2019 - 2024. All rights reserved.