【发布时间】:2020-08-17 14:00:44
【问题描述】:
我正在研究 cs50 pset4 恢复,进展顺利。但是,当我使用 if 语句检查该文件是否为 JPEG 时,clang 开始编写一些奇怪的错误消息。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *pFile;
char *buffer = NULL;
char *filename = NULL;
// If user didn't print 2 items
if (argc != 2) {
printf("Usage: ./recover image\n");
return 1;
}
// Open the file
pFile = fopen(argv[1], "r");
int j = 0;
// checking the card by 512b chunks
// loop (i=0, i++);
while (5 < 6) {
int i = 0;
i++;
// k=fread (buffer, 512, i, *file);
int k = fread(buffer, 512, i, pFile);
// if buffer [0]== 0xff // checking if it's the header. If yes - creating a new jpeg; if not -
// i++
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0) {
// if it's not the first file, we should close the last one
// sprintf
sprintf(filename, "%03i.jpg", 2);
// FILE = fopen (W)
pFile = fopen(filename, "w");
// fwrite (buffer, 512, j, *file1)
fwrite(buffer, 512, j, pFile);
// j=j+1
j = j + 1;
}
// if k<512 - end of the loop
if (k < 512) {
return 0;
}
}
}
我还没有完成剩下的所有工作,但是当我看到这些错误消息时我停下了:
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow recover.c -lcrypt -lcs50 -lm -o recover
recover.c:29:15: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
~~~~~~~~~ ^ ~~~~
recover.c:29:36: error: result of comparison of constant 216 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
~~~~~~~~~ ^ ~~~~
recover.c:29:57: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
~~~~~~~~~ ^ ~~~~
3 errors generated.
<builtin>: recipe for target 'recover' failed
make: *** [recover] Error 1
我尝试使用 help50,但发生了这种情况:
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow recover.c -lcrypt -lcs50 -lm -o recover
recover.c:29:15: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
~~~~~~~~~ ^ ~~~~
recover.c:29:36: error: result of comparison of constant 216 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
~~~~~~~~~ ^ ~~~~
recover.c:29:57: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
~~~~~~~~~ ^ ~~~~
3 errors generated.
<builtin>: recipe for target 'recover' failed
make: *** [recover] Error 1
Asking for help...
recover.c:29:15: error: result of comparison of constant 255 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
Not quite sure how to help, but focus your attention on line 29 of recover.c!
当我尝试谷歌搜索时,我只是得到了整个问题的答案。我不知道该怎么办,请帮帮我。
谢谢, 迷失在代码中:)
【问题讨论】:
-
字符从 -128 变为 127(在您的编译器上)。
-
Ohhhhh 好的,但我怎样才能修复此代码以使其正确并使其正常工作?我不能使用字符串,因为它们删除了 cs50.h “训练轮”。
-
如果您不处理字符串,为什么要使用
string?检查您的教科书以获取大小为 1 的替代数据类型。 -
关于:
char *buffer = NULL;和char *filename = NULL;分配/读取这些指针指向的位置(地址 0)的任何内容是引发 seg 故障事件的好方法。 (您不想这样做)建议:#define MAX_BUF_LEN 512#define MAX_FILENAME_LEN 30 .... char buffer[ MAX_BUF_LEN ];` 和char filename[ MAX_FILENAME_LEN ] ; -
OT:关于:
printf("Usage: ./recover image\n");错误消息应该输出到stderr,而不是stdout。建议:fprintf( stderr, "USAGE: %s imageFile\n", argv[0] );