CS50 反向 PSET 5 result1 不返回 1 [已关闭]

cs50 reverse pset 5 result1 dont return 1 [closed]

提问人:JamieForster 提问时间:11/11/2023 最后编辑:JamieForster 更新时间:11/11/2023 访问量:59

问:


想改进这个问题吗?通过编辑这篇文章添加详细信息并澄清问题。

10天前关闭。

  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

评论

0赞 Alex 11/11/2023
嘿,也许添加 C 作为标签或看看 cs50.stackexchange.com
0赞 Some programmer dude 11/11/2023
OT:应该是.的值是数组中有效元素的数量,包括索引处的第一个元素。相关说明:被指定为指针。if (argc < 2)if (argc < 3)argcargv0argv[argc]NULL
0赞 Eric Postpischil 11/11/2023
您的问题不清楚。 不是一个函数,所以它不能返回任何东西。你的意思是返回和赋值不是 1 吗?回复“这是错误消息 result1 不返回 1”:这是错误消息的确切文本吗?错误消息是语法不正确的“result1 don't return 1”?如果不是,请编辑问题以显示错误消息的完整确切文本,包括周围的上下文。此外,编辑问题以提供最小的可重现示例。如果没有 .result1freadresult1wav.h
0赞 JamieForster 11/11/2023
result1 保存 fread 的返回值,因此它确实返回了它假设返回 1 的东西,如果不是 fread 没有正确完成,感谢您的另一条评论
0赞 JamieForster 11/11/2023
反向/ $ ./反向输入.WAV 输出.WAV 结果 1 不等于 1

答:

1赞 chqrlie 11/11/2023 #1

存在多个问题:

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

    FILE *wav = fopen(file_name, "rb");
    
  • 使用 : 倒带流后,没有理由从流中重新读取每个像素的位数,这肯定会产生无效的计数。相反,您可能需要处理字节序来转换字段,以防您当前的平台不使用小字节序编码。你应该找回那个位置,而不是.fseek(wav, 0, SEEK_SET)header.bitsPerSamplesizeof(header)0

  • 测试似乎不正确。你的意思可能是if (num_samples / 2 != 0)if (num_samples % 2 != 0)

  • 假设每个样本和立体声至少有 16 位(即:numChannels == 2),但应使用标头字段来获取此信息并处理更通用的内容。

这是修改后的版本:

#include <errno.h>
#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 < 3) {
        fprintf(stderr, "Invalid usage: missing command line argument input file names\n");
        return 1;
    }

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

    char *file_name = argv[1];
    FILE *wav = fopen(file_name, "rb");
    if (wav == NULL) {
        // Error message
        fprintf(stderr, "Cannot open %s: %s\n", file_name, strerror(errno));
        return 1;
    }

    // Read header
    // TODO #3
    WAVHEADER header; // Wav header

    // Read wav into address of header
    if (fread(&header, sizeof(WAVHEADER), 1, wav) != 1) {
        fprintf(stderr, "Cannot read WAV header from %s\n", file_name);
        fclose(wav);
        return 1;
    }

    // Use check_format to ensure WAV format
    // TODO #4
    if (check_format(header)) {
        fprintf(stderr, "%s is not a WAV file\n", file_name);
        fclose(wav);
        return 1;
    }

    // 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");
    if (outfile == NULL) {
        fprintf(stderr, "Cannot open %s: %s\n", outfile_name, strerror(errno));
        fclose(wav);
        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
    long pos = ftell(wav);
    fseek(wav, 0, SEEK_END); // end of file
    long file_size = ftell(wav); // current file position
    fseek(wav, pos, SEEK_SET); // back to begining of samoples

    // Sample size (mono, stereo or more channels)
    int sample_size = (header.bitsPerSample / 8) * header.numChannels;
    long size = file_size - pos; // minus size of header
    long num_samples = size / sample_size;
    unsigned char *reversed = malloc(size);
    if (reversed == NULL) {
        fprintf(stderr, "cannot allocate %ld bytes for reversed array\n", size);
        fclose(wav);
        fclose(outfile);
        return 1;
    }

    long result1 = fread(reversed, sample_size, num_samples, wav);
    if (result1 != num_samples) {
        fprintf(stderr, "error reading samples from %s\n", file_name);
        fclose(wav);
        fclose(outfile);
        return 1;
    }

    for (long i = 0, j = num_samples - 1; i < j; i++, j++) {
        unsigned char sample[sample_size];
        memcpy(sample, &reversed[i * sample_size]);
        memcpy(&reversed[i * sample_size], &reversed[j * sample_size], sample_size);
        memcpy(&reversed[j * sample_size], sample, sample_size);
    }
    int result2 = fwrite(reversed, sample_size, num_samples, outfile);
    if (result2 < num_samples) {
        fprintf(stderr, "error writing samples to %s\n", outfile_name);
    }

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

int check_format(WAVHEADER header) {
    // TODO #4
    if (memcmp(header.format, "WAVE", 4) == 0) {
        return 0;
    } else {
        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;
}

评论

0赞 JamieForster 11/11/2023
我实现了这个,它仍然给我同样的错误