<a target="_blank" href="http://mwultong.blogspot.com/2007/04/c-hex-view-file-dump-source-code.html" style="color:#0000ff;text-decoration:none;font-family:gulim;line-height:17.2800006866455px;">http://mwultong.blogspot.com/2007/04/c-hex-view-file-dump-source-code.html</a> <div>에서 나온 소스를 함수를 1개 더 추가해서 결과가 같도록 표현해보려고 했었는데요..(직접 입력 안받고 test화일을 읽는 점만 다릅니다.)</div> <div>원래는 작성하다가 컴파일도 않되어서 확인해보니 코드가 길어지면서 잘못된 문장이 들어간것 같아서 이번에는 컴파일이 되도록 수정한 것 같은데요.</div> <div>아래처럼 수정 되었는데 그런데...</div> <div>결과가 16개씩 않나오고 이상하게 1/4로 줄어서 문자열 HEX열 모두 4개씩 나옵니다.</div> <div>test.txt 파일에 ABCDEFGHIJKLMNOPQRSTUVWXYZ..abcdefghijklmnopqrstuvwxyz..1234567891011121314151617181920</div> <div>를 넣고 실행하면 결과가 아래처럼 나옵니다.</div> <div>원래는 hex값 문자열 모두 16개씩(16열) 나와야 하는데 아래처럼 4개씩 밖에 않나옵니다.ㅡ.ㅜ;</div> <div><span style="font-size:9pt;line-height:1.5;">00000000: 41 42 43 44 ABCD</span></div> <div><span style="line-height:14.3999996185303px;">00000010: 45 46 47 48 EFGH</span></div> <div><span style="line-height:14.3999996185303px;">00000020: 49 4A 4B 4C I J KL </span></div> <div><span style="line-height:14.3999996185303px;">00000030: 4D 4E 4F 50 MNOP</span></div> <div><span style="line-height:14.3999996185303px;">00000040: 51 52 53 54 QRST</span></div> <div><span style="line-height:14.3999996185303px;">00000050: 55 56 57 58 UVWX</span></div> <div><span style="line-height:14.3999996185303px;">00000060: 59 5A 2E 2E YZ . .</span></div> <div><span style="line-height:14.3999996185303px;">00000070: 61 62 63 64 abcd</span></div> <div><span style="line-height:14.3999996185303px;">00000080: 65 66 67 68 efgh</span></div> <div><span style="line-height:14.3999996185303px;">00000090: 69 6A 6B 6C i j k l</span></div> <div><span style="line-height:14.3999996185303px;">000000A0: 6D 6E 6F 70 mnop</span></div> <div><span style="line-height:14.3999996185303px;"><br></span></div> <div><span style="line-height:14.3999996185303px;">....................................</span></div> <div><span style="line-height:14.3999996185303px;">..................</span></div> <div><span style="font-size:9pt;line-height:1.5;">진짜 미친놈처럼 별의 별별 생쇼를 몇시간에 걸쳐서 했지만 수정되지 않네요</span></div> <div><span style="font-size:9pt;line-height:1.5;">.ㅜ.ㅜ;</span></div> <div>왜 이 소스 컴파일해서 cmd창에서 실행하면 결과가 16열씩 안나오고 4열씩 나오는 이유가 뭔지 아시는분 답변 주시면 정말 고맙겠네요.ㅜ.ㅜ</div> <div>부탁드립니다.ㅜ.ㅜ;ㅜ.ㅜ;ㅜ.ㅜ;ㅜ.ㅜ;</div> <div><div><br></div> <div>#include <stdio.h></div> <div>#include <stdlib.h></div> <div>#include <iostream></div> <div>using namespace std;</div> <div><br></div> <div>int hexView(const char* filename);</div> <div>void Show_hexpage(char* buf16,FILE *in, size_t buf16Len,size_t offset);</div> <div><br></div> <div><br></div> <div><br></div> <div>void Show_hexpage(char* buf16,FILE *in, size_t buf16Len,size_t offset){</div> <div> // 파일을 16바이트씩, 끝까지 읽으며 루프 돌리기</div> <div> while ( (buf16Len = fread(buf16, sizeof(char), sizeof(buf16), in)) != NULL ) </div> <div> {</div> <div> printf("%08X: ", offset); // Offset (번지) 출력</div> <div> // 헥사 부분의 헥사 값 16개 출력 (8개씩 2부분으로)</div> <div> for (int i = 0; i < (int) buf16Len; i++) </div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div> if (i == 8) printf(" "); // 8개씩 분리</div> <div> printf("%02X ", (unsigned char) buf16[i]); // 헥사 값 출력</div> <div> }</div> <div><br></div> <div> // 한 줄이 16 바이트가 되지 않을 때, 헥사 부분과 문자 부분 사이에 공백들 삽입</div> <div> for (int i = 0; i <= (16 - (int) buf16Len) * 3; i++)</div> <div> printf(" ");</div> <div> if (buf16Len < 9) printf(" "); // 한줄이 9바이트보다 적을 때는 한칸 더 삽입</div> <div><br></div> <div> // 문자 부분 출력</div> <div> for (int i = 0; i < (int) buf16Len; i++) </div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div> if (buf16[i] >= 0x20 && buf16[i] <= 0x7E) // 특수 문자 아니면 출력</div> <div> printf("%c", buf16[i]);</div> <div> else printf("."); // 특수문자, 그래픽문자 등은 마침표로 출력</div> <div> }</div> <div><br></div> <div> offset += 16; // 번지 값을 16 증가</div> <div> printf("\n"); // 줄바꿈</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span></div> <div> }</div> <div>}</div> <div>int hexView(const char* filename)</div> <div>{</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>FILE *in;</div> <div><br></div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>size_t offset = 0; // 번지</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>char buf16[16]; // 헥사 값이 16바이트, 즉, 한줄씩 저장될 버퍼</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>size_t buf16Len=0; // 한줄에 들어있는 헥사 값의 개수, 즉, 길이</div> <div><br></div> <div> if (fopen_s(&in, filename, "rb") != 0) {</div> <div> fputs("파일 오픈 에러\n", stderr);</div> <div> return 1;</div> <div> }</div> <div> Show_hexpage(buf16,in, buf16Len,offset);</div> <div><br></div> <div> if (offset == 0) printf("%08X: ", offset); // 0바이트 파일일 경우 처리</div> <div> fclose(in); // 파일 닫기</div> <div><br></div> <div> return 0;</div> <div>}</div> <div>int main(int argc, char* argv[])</div> <div>{</div> <div>if (argc == 0)</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>fputs("사용법: hexView 파일명\n", stderr);</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>exit(1);</div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div> <div><br></div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>hexView("test.txt");</div> <div><br></div> <div><span class="Apple-tab-span" style="white-space:pre;"> </span>return 0;</div> <div>}</div> <div><br></div></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.