을 찾는 특정 바이트에서 파일

0

질문

나는 파일에서는 내가 보는 이의 시퀀스를 바이트:0xFF,0xD8,0xFF,그리고 0xE0. 을 위해 지금 가정하자 나는 보 0xFF. 이 프로그램에 대한 테스트:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void analyzeFile(char* filename)
{
    FILE* filePtr = fopen(filename, "rb");

    int numImages = 0;

    while (!feof(filePtr))
    {
        char bytes;

        bytes = getc(filePtr);

        printf("%c", bytes);

        if ((bytes == 0xFF))
        {
            numImages++;
            printf("image found!\n");
        }
    }

    printf("%d\n", numImages);
}

이 작동하지 않습니다. 부를 때 analyzeFile 매개 변수"test.txt"인쇄 파일의 내용을 밖으로 좋은,하지만 감지하지 못하는 단일 0xFF 바이트:

내용 test.txt:

aÿØÿÿà1234

출력:

aÿØÿÿà1234
0

에 대한 참조,0xFF 에 해당하 y 분음기호,ÿ 에 따라,ASCII.

c char file
2021-11-23 15:01:14
1

최고의 응답

0

두 가지 문제가 있으로 귀하의 코드입니다. 첫 참조하십시오: 왜"while(!feof(파일)가)항상"잘못입니까?

두 번째 문제는 getc (나 fgetc)반환합니다 intchar. 그대로,당신 char 의 가치 0xFF 은 sign-extended(하기 0xFFFFFFFF가장 높)을 때 그것은 승진하는 intif ((bytes == 0xFF)) 비교입니다. 그래서 사용 intbytes 변수와 루프를 변경 테스트 값 읽기에 대한 EOF 신호:

void analyzeFile(char* filename)
{
    FILE* filePtr = fopen(filename, "rb");
    if (!filePtr) { // Add some error handling...
        printf("Could not open file!");
        return;
    }
    int numImages = 0;
    int bytes;
    while ( ( bytes = getc(filePtr) ) != EOF) {
        printf("%02X %c\n", (unsigned)bytes, bytes);

        if (bytes == 0xFF) { // Removed redundant extra parentheses
            numImages++;
            printf("image found!\n");
        }
    }
    fclose(filePtr); // Don't forget to close the file!
    printf("%d\n", numImages);
}
2021-11-23 17:14:53

관련 질문,하지만 캐스팅 (char)bytes 으로 매개 변수 printf 쓸모없는? 그것을 얻을 것이 casted 시 int 기본 프로모션이다.
Eugene Sh.

도움을 주셔서 감사합니다!
human bean

의 가치 (char)bytes 가 정의된 구현하는 경우 bytes > CHAR_MAX (수 있는 경우에만 발생 char 서명된 형식),그래서 아마도 더 나은지를 캐스팅 char. 또한, %X 기대하는 unsigned int다,그래서 당신이 필요 캐스팅(하기 unsigned int의)는 하나입니다.
Ian Abbott

@IanAbbott 공정한 코멘트 편집을 참조하십시오.
Adrian Mole

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................