- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对包含多个 JPEG 的文件使用 fread 并将 JPEG 写入新文件,但在执行此操作之前,我需要正确查看该文件并根据其第一个字节查找 JPEG if 语句位于下面代码的底部。
我无法进入 if 语句,并一直尝试打印出字节,但在打印时遇到了问题。
我希望只打印缓冲区的 0 字节,但我的输出如下所示:711151a6cec117f07603c9a973599166
我对 C 和 fread 非常陌生,如果有任何帮助,我们将不胜感激!
代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Check for 2 arguments, the name of the program and the file being read
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
else
{
//Open the file
FILE * fp;
fp = fopen(argv[1], "r");
//Get file length
fseek(fp, 0, SEEK_END);
int f_length = ftell(fp);
fseek(fp, 0, SEEK_SET);
// If not file is found then exit
if(fp == NULL)
{
printf("File not found\n");
return 2;
}
// Allocate buffer for fread function
int *buffer = (int*)malloc(f_length);
if (buffer == NULL)
{
printf("Buffer is null\n");
return 1;
}
// Read thorugh the file
while(fread(buffer, 512, 1, fp) == 1)
{
for (int i = 0; i < 1; i++)
{
printf("%x\n", buffer[i]);
}
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
printf("Found a jpg\n");
}
}
// Exit the program
return 0;
}
}
最佳答案
int *buffer
不正确,因为其目的是处理字节而不是整数。如果使用 int *
,则 buffer[0]
将是前 4 个字节,而不是预期的第一个字节。将其更改为unsigned char *buffer
。
所以明确地,该行应该如下(包括删除不必要的强制转换):
unsigned char *buffer = malloc(f_length);
关于c - 如何正确打印 JPEG 文件的字节? - CS50 PSET3 恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384556/
我不知道为什么这不能编译,对我来说看起来不错。想要制作一个程序来构建像 super 马里奥关卡末尾那样的金字塔。会询问用户 获取一个数字,然后将金字塔 build 到该高度。 #include #i
我以为我已经完成了 Caesar,但是当运行 CHeck50 时,我的代码因以下原因失败:使用 23 作为 key 将“barfoo”加密为“yxocll”输出无效的 ASCII 文本日志运行./凯撒
我对编程真的很陌生,我刚刚开始在哈佛的 CS50 上尝试一些问题集。如果有人能向我指出为什么我的代码是错误的,我将不胜感激。 编译并运行代码后,没有任何输出。 另一方面,有人可以向我解释一下“roun
我意识到,当我在 bool 搜索中包含“else return false”时,它永远无法“找到针”。相反,如果我要删除那部分,程序就可以正常工作。它能够找到 2008 而找不到 2013。知道为什么
我正在编写一个程序,它接受输入并打印出使用的最少数量的硬币。当我运行程序并输入内容时,它没有按预期工作并且不打印任何内容。我在这里做错了什么? #include #include int main
我目前正在尝试CS50的Pset2,在caesar.c中,用户应该通过argv输入k;如果他不这样做,我就应该“大喊大叫”用户告诉他使用命令行参数。但如果我尝试以下操作,我总是会遇到段错误。 #inc
问题集要求我们使用哈希创建一个半金字塔。这是它的外观图像的链接 - 我明白了这个想法并编写了程序,直到打印空格(我已将其替换为“_”,以便我可以测试它的前半部分。 但是,当我尝试运行我的程序时,它不会
我正在尝试解决problem 1A on codeforces 但我不断收到测试:#1,时间:0 毫秒,内存:1828 KB,退出代码:1,检查器退出代码:0,结论:RUNTIME_ERROR你可以查
我目前正在使用 C 语言研究 CS50 中的维吉尼亚密码。要求是制作一个程序,根据关键字(两者都是用户输入的)对一些明文进行加密。它将基于维吉尼亚密码进行加密。我发现很难用语言描述 Vigenere
这是 edX.org 上 CS50 类(class)的 PSET 3。 我已经为这个问题集苦苦挣扎了很长时间;特别是,我无法使 binarySearch 函数工作。我一直遇到段错误,但我不知道如何处理
我正在研究 Pset 2 hack.c,到目前为止,我已经设法了解了总体概念。但是,我的代码仍然不起作用。它编译并运行但不打印任何内容。 我不完全确定这里出了什么问题,我可能忽略了一些东西? #inc
我陷入了调整大小问题的垂直调整大小部分。我从 Zamayla 的伪代码中知道,我每次都需要在输出文件上写入一个数组,但我不知道如何将值从一个传递到另一个。我是否需要使用 malloc 函数并通过指针传
我遵循了 pset 3 recover 的伪代码,我的代码只输出一个图像,调试器 (debug50) 在 number = fread(buffer, 1, 512, file); 中循环 4 次后退
我是这个主题的新手。我尝试自己调试此问题,但是出现了段错误核心转储,我无法弄清楚原因。有人可以帮我吗? # include # include # include # include int main
我正在尝试制作一个程序,提供最少数量的硬币找零,但如果我给出的数字不是可分为四等分的数字,它就会惨败。例如,如果我输入 1.25,我会得到 5 个 25 美分,但如果我输入 1.26,我会得到 5 个
#include #include #include "bmp.h" int main(int argc, char *argv[]) { // ensure proper usage
我对 cs50 pset 1(马里奥不太舒服)的解决方案没有打印出金字塔。相反,它只是打印出一个#。我已经尝试了多次,但我在尝试编译时得到的只是错误,说它不能识别 int i 或者分号应该移到新行。更
我创建了以下代码作为对 CS50x PSET2: Vigenere 的回答,它在某种程度上有效,但是当运行 check50 时,我得到了下面列出的一些错误: :) vigenere.c exists.
我不太明白这些错误从何而来。我正在尝试创建一个简单的 C 程序,它接受一个字符串并向 ASCII 值添加偏移量,以便创建一个极其简单的加密。 #include #include #include
我目前正在学习 CS50 类(class),但我完全陷入了调整大小的问题(不太舒服)。任务是制作一个程序,该程序接受输入 .bmp 文件,将其缩放 n 次并将其写入输出 .bmp 文件。但我的代码只是
我是一名优秀的程序员,十分优秀!