如何解决错误:常数255与'char'类型的表达式的比较结果始终为false
我正在研究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,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,j,*file1)
fwrite(buffer,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)
~~~~~~~~~ ^ ~~~~
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进行比较是徒劳的。因此,使其unsigned char *buffer
至少存储0到255。
还
隐式转换将失去整数精度:将'size_t'(aka'unsigned long')转换为'int'
是说明性的:size_t
在内部是unsigned long
,比int
可以存储更多的正数。因此,理论上您可能会出现int溢出。
类似的问题,但我的答案是C ++:
,将缓冲区声明为uint8_t缓冲区[512],因为未分配空间来容纳512个字节。 完成操作后,请记住关闭文件,例如fclose(pFile)
,在MSCV编译器上,我抱怨
while (5 < 6)
第23行,
警告C4127:条件表达式恒定
,它确实是一个常数。您可以更改
int k = fread(buffer,512,i,pFile);
第29行,
从“ size_t”到“ int”的转换,可能会丢失数据
fread()
返回size_t
无符号且k
为int
,因此如果fread()
读取的字节数超过INT_MAX
个字节,则它可以变为负数
在这里我们也
“缓冲区”可能为“ 0”:这不符合以下规范 函数“ fread”
确保它出现在其他部分。您已将buffer
声明为
char* buffer = NULL;
并且从未为其分配内存。
我改变了
char _buffer[512];
char* buffer = _buffer;
和while
到
while (pFile) // pFile is not 0
因为pFile
是指打开的文件,所以它是真的
并将演员表添加到fread()
int k = (int) fread(buffer,(size_t)i,pFile);
所以它编译正常
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。