c语言 - 错误:常量 255 与类型为 'char' 的表达式的比较结果始终为假



我正在进行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 imagen");
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!

当我试着在谷歌上搜索时,我只得到了整个问题的答案。我不知道该怎么办,请帮帮我。

谢谢,丢失在代码中:(

char在此实现中的范围从-127到127(最小值(。因此,将其与216或255进行比较是徒劳的。因此,使其成为至少存储0到255的unsigned char *buffer

此外,

隐式转换丢失整数精度:"size_t"(又名"unsigned long"(到"int">

说明:size_t在内部是unsigned long,它可以存储比int更多的正数。所以理论上可能存在int溢出。

  • https://en.wikipedia.org/wiki/Integer_overflow

  • https://en.wikipedia.org/wiki/C_data_types#Main_types


类似的问题,但我的答案是C++:

  • 如何保存重新编码的数据

将buffer声明为uint8_t buffer[512],因为没有分配空间来容纳512个字节。记得在完成文件后关闭文件,例如fclose(pFile(

在MSCV编译器上,我收到了关于的投诉

while (5 < 6)

在23号线上,

警告C4127:条件表达式是常量

并且它确实是一个常数。你可以把它换成

int k = fread(buffer, 512, i, pFile);

在29号线上,

从"size_t"到"int"的转换,可能会丢失数据

fread()返回无符号的size_t,而kint,因此如果fread()读取的字节数超过INT_MAX,则它可以变为负数。

这里还有

"缓冲区"可能为"0":这不符合功能‘fread’

确保它出现在其他部分。您已将buffer声明为

char* buffer = NULL;

并且从未为其分配内存。

我更改了

char _buffer[512];
char* buffer = _buffer;

和CCD_ 13到

while (pFile) // pFile is not 0 

由于pFile是指打开的文件,因此它是真正的

并为CCD_ 15 添加了一个铸件

int k = (int) fread(buffer, 512, (size_t)i, pFile);

所以它编译了ok

最新更新