首页 > 解决方案 > 读取的文件数始终为 0

问题描述

这是我写的一个小代码。请注意 -

  1. rawbytes> 512字节指向的文件
  2. 我是在“r”还是“rb”中打开正在读取的文件?除了几个 JPEG,我不知道里面有什么。
  3. 为什么我的 fread 总是输出 0,即使我把它设为 fread(bytes, 1, 512, rawbytes)?

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

int main(int argc, string argv[])
{
    // Declaring a new type 
    typedef uint8_t BYTE;
    
    // Declaring an array of bytes, in which I want to read 512 bytes
    BYTE bytes[512];
    
    FILE *rawbytes = fopen(argv[1], "rb");
    int numberofbytesread = fread(bytes, 1, 512, rawbytes);
    
    // Check
    printf("%i\n", numberofbytesread);
    fclose(rawbytes);
}

编辑 1: 将 int 数据类型更改为 size_t 数据类型。新代码:

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

int main(int argc, string argv[])
{
    // Declaring a new type 
    typedef uint8_t BYTE;

    // Declaring an array of bytes, in which I want to read 512 bytes
    BYTE bytes[512];

    FILE *rawbytes = fopen(argv[1], "rb");
    size_t numberofbytesread = fread(bytes, 1, 512, rawbytes);

    // Check
    printf("%zu\n", numberofbytesread);
    fclose(rawbytes);
}

我不知道 %zu 是什么,但编译器告诉我将 %i 与 %zu 交换。仍然读取 0 字节。:(

编辑 2:为了使问题可重现,我已将 argv[1] 替换为存储卡文件,我的任务是从被调用的card.raw. 这段代码只是我写的一小段代码,用于检查我是否能够从其中读取至少 512 个字节card.raw

执行wget https://cdn.cs50.net/2019/fall/psets/4/recover/recover.zip以下载包含此问题分发的(压缩)ZIP 文件。执行unzip recover.zip以解压缩该文件。

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

int main(int argc, string argv[])
{
    // Declaring a new type 
    typedef uint8_t BYTE;

    // Declaring an array of bytes, in which I want to read 512 bytes
    BYTE bytes[512];

    FILE *rawbytes = fopen("card.raw", "rb");
    if (rawbytes == NULL)
    {
           return 1;
    }

    size_t numberofbytesread = fread(bytes, 1, 512, rawbytes);

    // Check
    printf("%zu\n", numberofbytesread);
    fclose(rawbytes);
}

标签: ccs50fread

解决方案


这里似乎还可以

我在这里尝试过,它适用于本地文件。然后我尝试了这个card.raw,它似乎也可以。第一个块只有零,第二个有一个惊喜,然后它继续数据。

第二块

  • 第三块似乎是一个有效的 jpeg 签名......

在此处输入图像描述

  • 除了显示数据,我没有更改您的代码
  • 在 CL 19.27 下编译。请不要在这里讨论宗教。
  • 只要有数据,代码就会读取...等待块之间的键

这是代码

#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
//#include <cs50.h>

int main(int argc, char** argv)
{
    // Declaring a new type 
    typedef uint8_t BYTE;

    // Declaring an array of bytes, in which I want to read 512 bytes
    BYTE bytes[512];

    FILE* rawbytes = fopen("card.raw", "rb");
    if (rawbytes == NULL) return 1;
    // ok file is open
    size_t numberofbytesread = 0;
    size_t block = 0;
    char ch;
    while ((numberofbytesread = fread(bytes, 1, 512, rawbytes)) > 0)
    {
        printf("\n%zu bytes read:\n\n\t000    ", numberofbytesread);
        int col = 0;
        for (size_t i = 0; i < numberofbytesread; i += 1)
        {
            if(isprint(bytes[i]))
                printf("%2c  ", bytes[i]);
            else
                printf("%02X  ", bytes[i]);
            col += 1;
            if (col % 16 == 0) printf("\n\t%3d    ",i+1);
        };  // for()
        if (col % 16 == 0) printf("\n");
        block += 1;
        printf("Block: %zd. Press ENTER \n", block); 
        ch = fgetc(stdin);
    };
    fclose(rawbytes);
}

推荐阅读