전에 올린 파일포인터 관련 질문입니다.
글쓴이: esaram5 / 작성시간: 일, 2021/12/19 - 6:03오후
제가 분석하려는 파일은 40MB쯤 되는 ZIP파일 입니다. Visual studio 에서 디버깅을 할 때 find_end_C_D함수에서 파일을 끝까지 읽지 않고 중간에 while 문을 종료합니다. 크기가 작은 파일을 이용해 봐도 끝까지 탐색하지 않는데 어디가 잘못된 것인지 알 수 있을까요? 종료되는 지점은 HxD에서 hex로 파일을 읽었을 때 offset이 450인 지점이였습니다. 작은 파일일때는 30kb정도의 파일을 사용하고 offset은 150에서 끝났습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <Windows.h>
#pragma pack(1)
typedef unsigned short ushort;
typedef unsigned long ulong;
int find_end_C_D(FILE* file) {
ulong signature=0;
while (feof(file) ) {
fread(&signature, sizeof(signature), 1, file);
if (signature != 0x06054b50) {
printf("%08x\n", signature);
continue;
}
}
}
int main(){
FILE* zip_file = 0;
zip_file = fopen("Downloads.zip", "r");
if (!zip_file) {
perror("fopen:");
return 0;
}
find_end_C_D(zip_file);
}Forums:


"r" 을 "rb"로 해보시고,
"r" 을 "rb"로 해보시고,
while (feof(file) ) 을 while ((result = feof(file)) == 0) 로 해보시고
한번 step-by-step으로 디버그를 해보세요. result 값을 모니터링(watch) 해 보세요.
fopen에서 인수를 rb로 바꾸는것으로 오류를 수정했습니다.
fopen에서 r과 rb의 차이가 뭔지 알 수 있을까요?
File access mode flag "b" can
File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has no effect on POSIX systems, but on Windows, for example, it disables special handling of '\n' and '\x1A'.
https://en.cppreference.com/w/cpp/io/c/fopen 에 의하면 Windows에서는 r과 rb를 구분한다라고 되어 있습니다. 아마 이 때문에 코드 동작에 차이가 생긴 것이 아닌가라는 생각이 듭니다.
...
... while ( !feof(file) ) { ...$ man feof ... The feof() function returns nonzero if the end-of-file indicator is set for stream; otherwise, it returns zero. ...댓글 달기