0

ストリームの最初のバイトを比較する必要がある小さなプロジェクトがあります。問題は、そのバイトが 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; 
}

うまくいきません。中にクエスチョン マークが付いた黒い 6 面のひし形を示すような文字が表示されます...理論的には次の文字のみを許可する必要がありましたSpace (32), 0-9 (48-57), A-Z (65-90), a-z (97-122):

ctype.h -> iscntrl、isalnum、ispunct の関数を使用してみましたが、これも機能しませんでした。

単純な C の問題であると私が想定していることで、仲間の C 初心者を助けることができる人はいますか? それは大歓迎です!

ありがとう。マーティン

4

1 に答える 1

7

なぜintにキャストしているのかわかりません。次のいずれかを使用することを検討してください。

if ((entry[0] >= 'A' && entry[0] <= 'Z') ||
    (entry[0] >= 'a' && entry[0] <= 'z') ||
    entry[0] == ' ' || entry[0] == '.')

また

#include <ctype.h>
if (isalnum(entry[0]) || entry[0] == ' ' || entry[0] == '.')
于 2011-04-26T03:23:02.490 に答える