c - 读取的文件数始终为 0
问题描述
这是我写的一个小代码。请注意 -
rawbytes
> 512字节指向的文件- 我是在“r”还是“rb”中打开正在读取的文件?除了几个 JPEG,我不知道里面有什么。
- 为什么我的 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);
}
解决方案
这里似乎还可以
我在这里尝试过,它适用于本地文件。然后我尝试了这个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);
}
推荐阅读
- typescript - 打字稿:创建具有相同键但不同值的对象
- r - 在 R 控制台输出中右对齐字符串字符列
- java - 如何在 Java 中创建阻塞线程安全流?
- php - MySQL:如何只选择每个不同的 x-column-name 中的三个
- java - 无法读取文档 URL:无法读取整个标题;读取 6 个字节;预期 32 字节
- animation - SVG - animateMotion 不适用于 Firefox 中的 USE 元素
- python - MySQL:使用“SELECT INTO OUTFILE”时对字符串进行wiered chopping
- java - 数据源在 WebLogic 12.2 上无故丢失
- telegram - 如何确保chat_id 存在?
- c# - 如何修复路径“/”的控制器未找到或未实现 IController