我正在做一个小项目,需要比较流的第一个字节。问题是该字节可以是 0xe5 或任何其他不可打印的字符,因此表示该特定数据是错误的(一次读取 32 位)。我可以允许的有效字符是 AZ、az、0-9、'.' 和空间。
当前代码是:
FILE* fileDescriptor; //assume this is already open and coming as an input to this function.
char entry[33];
if( fread(entry, sizeof(unsigned char), 32, fileDescriptor) != 32 )
{
return -1; //error occured
}
entry[32] = '\0'; //set the array to be a "true" cstring.
int firstByte = (int)entry[0];
if( firstByte == 0 ){
return -1; //the entire 32 bit chunk is empty.
}
if( (firstByte & 0xe5) == 229 ){ //denotes deleted.
return -1; //denotes deleted.
}
所以问题是当我尝试执行以下操作时:
if( firstByte >= 0 && firstByte <= 31 ){ //NULL to space in decimal ascii
return -1;
}
if( firstByte >= 33 && firstByte <= 45 ){ // ! to - in decimal ascii
return -1;
}
if( firstByte >= 58 && firstByte <= 64 ) { // : to @ in decimal ascii
return -1;
}
if( firstByte >= 91 && firstByte <= 96 ) { // [ to ` in decimal ascii
return -1;
}
if( firstByte >= 123 ){ // { and above in decimal ascii.
return -1;
}
它不起作用。我看到诸如表示黑色六面菱形的字符,里面有一个问号......理论上它应该只让以下字符:Space (32), 0-9 (48-57), A-Z (65-90), a-z (97-122)
,但我不知道为什么它不能正常工作。
我什至尝试使用 ctype.h -> iscntrl、isalnum、ispunct 中的函数,但这也没有用。
任何人都可以帮助我认为是一个简单的c问题的c新手吗?这将不胜感激!
谢谢。马丁